-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,367 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist | ||
node_modules | ||
node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import http from 'http'; | ||
|
||
const CALLBACK_URL = process.env.CALLBACK_URL ? new URL(process.env.CALLBACK_URL) : null | ||
const CALLBACK_TIMEOUT = process.env.CALLBACK_TIMEOUT || 5000 | ||
const CALLBACK_OBJECTS = process.env.CALLBACK_OBJECTS ? JSON.parse(process.env.CALLBACK_OBJECTS) : {} | ||
|
||
export const isCallbackSet = !!CALLBACK_URL | ||
|
||
/** | ||
* @param {Uint8Array} update | ||
* @param {any} origin | ||
* @param {WSSharedDoc} doc | ||
*/ | ||
export const callbackHandler = (update, origin, doc) => { | ||
const room = doc.name | ||
const dataToSend = { | ||
room, | ||
data: {} | ||
} | ||
const sharedObjectList = Object.keys(CALLBACK_OBJECTS) | ||
sharedObjectList.forEach(sharedObjectName => { | ||
const sharedObjectType = CALLBACK_OBJECTS[sharedObjectName] | ||
dataToSend.data[sharedObjectName] = { | ||
type: sharedObjectType, | ||
content: getContent(sharedObjectName, sharedObjectType, doc).toJSON() | ||
} | ||
}) | ||
callbackRequest(CALLBACK_URL, CALLBACK_TIMEOUT, dataToSend) | ||
} | ||
|
||
/** | ||
* @param {URL} url | ||
* @param {number} timeout | ||
* @param {Object} data | ||
*/ | ||
const callbackRequest = (url, timeout, data) => { | ||
data = JSON.stringify(data) | ||
const options = { | ||
hostname: url.hostname, | ||
port: url.port, | ||
path: url.pathname, | ||
timeout, | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Content-Length': data.length | ||
} | ||
} | ||
const req = http.request(options) | ||
req.on('timeout', () => { | ||
console.warn('Callback request timed out.') | ||
req.destroy() | ||
}) | ||
req.on('error', (e) => { | ||
console.error('Callback request error.', e) | ||
req.destroy() | ||
}) | ||
req.write(data) | ||
req.end() | ||
} | ||
|
||
/** | ||
* @param {string} objName | ||
* @param {string} objType | ||
* @param {WSSharedDoc} doc | ||
*/ | ||
const getContent = (objName, objType, doc) => { | ||
switch (objType) { | ||
case 'Array': return doc.getArray(objName) | ||
case 'Map': return doc.getMap(objName) | ||
case 'Text': return doc.getText(objName) | ||
case 'XmlFragment': return doc.getXmlFragment(objName) | ||
case 'XmlElement': return doc.getXmlElement(objName) | ||
default : return {} | ||
} | ||
} |
Oops, something went wrong.