@heroiclabs/nakama-js
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -5,2 +5,179 @@ 'use strict'; | ||
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var base64 = createCommonjsModule(function (module, exports) { | ||
/*! http://mths.be/base64 v0.1.0 by @mathias | MIT license */ | ||
(function(root) { | ||
// Detect free variables `exports`. | ||
var freeExports = 'object' == 'object' && exports; | ||
// Detect free variable `module`. | ||
var freeModule = 'object' == 'object' && module && | ||
module.exports == freeExports && module; | ||
// Detect free variable `global`, from Node.js or Browserified code, and use | ||
// it as `root`. | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal; | ||
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { | ||
root = freeGlobal; | ||
} | ||
/*--------------------------------------------------------------------------*/ | ||
var InvalidCharacterError = function(message) { | ||
this.message = message; | ||
}; | ||
InvalidCharacterError.prototype = new Error; | ||
InvalidCharacterError.prototype.name = 'InvalidCharacterError'; | ||
var error = function(message) { | ||
// Note: the error messages used throughout this file match those used by | ||
// the native `atob`/`btoa` implementation in Chromium. | ||
throw new InvalidCharacterError(message); | ||
}; | ||
var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// http://whatwg.org/html/common-microsyntaxes.html#space-character | ||
var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g; | ||
// `decode` is designed to be fully compatible with `atob` as described in the | ||
// HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob | ||
// The optimized base64-decoding algorithm used is based on @atk’s excellent | ||
// implementation. https://gist.github.com/atk/1020396 | ||
var decode = function(input) { | ||
input = String(input) | ||
.replace(REGEX_SPACE_CHARACTERS, ''); | ||
var length = input.length; | ||
if (length % 4 == 0) { | ||
input = input.replace(/==?$/, ''); | ||
length = input.length; | ||
} | ||
if ( | ||
length % 4 == 1 || | ||
// http://whatwg.org/C#alphanumeric-ascii-characters | ||
/[^+a-zA-Z0-9/]/.test(input) | ||
) { | ||
error( | ||
'Invalid character: the string to be decoded is not correctly encoded.' | ||
); | ||
} | ||
var bitCounter = 0; | ||
var bitStorage; | ||
var buffer; | ||
var output = ''; | ||
var position = -1; | ||
while (++position < length) { | ||
buffer = TABLE.indexOf(input.charAt(position)); | ||
bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; | ||
// Unless this is the first of a group of 4 characters… | ||
if (bitCounter++ % 4) { | ||
// …convert the first 8 bits to a single ASCII character. | ||
output += String.fromCharCode( | ||
0xFF & bitStorage >> (-2 * bitCounter & 6) | ||
); | ||
} | ||
} | ||
return output; | ||
}; | ||
// `encode` is designed to be fully compatible with `btoa` as described in the | ||
// HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa | ||
var encode = function(input) { | ||
input = String(input); | ||
if (/[^\0-\xFF]/.test(input)) { | ||
// Note: no need to special-case astral symbols here, as surrogates are | ||
// matched, and the input is supposed to only contain ASCII anyway. | ||
error( | ||
'The string to be encoded contains characters outside of the ' + | ||
'Latin1 range.' | ||
); | ||
} | ||
var padding = input.length % 3; | ||
var output = ''; | ||
var position = -1; | ||
var a; | ||
var b; | ||
var c; | ||
var buffer; | ||
// Make sure any padding is handled outside of the loop. | ||
var length = input.length - padding; | ||
while (++position < length) { | ||
// Read three bytes, i.e. 24 bits. | ||
a = input.charCodeAt(position) << 16; | ||
b = input.charCodeAt(++position) << 8; | ||
c = input.charCodeAt(++position); | ||
buffer = a + b + c; | ||
// Turn the 24 bits into four chunks of 6 bits each, and append the | ||
// matching character for each of them to the output. | ||
output += ( | ||
TABLE.charAt(buffer >> 18 & 0x3F) + | ||
TABLE.charAt(buffer >> 12 & 0x3F) + | ||
TABLE.charAt(buffer >> 6 & 0x3F) + | ||
TABLE.charAt(buffer & 0x3F) | ||
); | ||
} | ||
if (padding == 2) { | ||
a = input.charCodeAt(position) << 8; | ||
b = input.charCodeAt(++position); | ||
buffer = a + b; | ||
output += ( | ||
TABLE.charAt(buffer >> 10) + | ||
TABLE.charAt((buffer >> 4) & 0x3F) + | ||
TABLE.charAt((buffer << 2) & 0x3F) + | ||
'=' | ||
); | ||
} else if (padding == 1) { | ||
buffer = input.charCodeAt(position); | ||
output += ( | ||
TABLE.charAt(buffer >> 2) + | ||
TABLE.charAt((buffer << 4) & 0x3F) + | ||
'==' | ||
); | ||
} | ||
return output; | ||
}; | ||
var base64 = { | ||
'encode': encode, | ||
'decode': decode, | ||
'version': '0.1.0' | ||
}; | ||
// Some AMD build optimizers, like r.js, check for specific condition patterns | ||
// like the following: | ||
if ( | ||
typeof undefined == 'function' && | ||
typeof undefined.amd == 'object' && | ||
undefined.amd | ||
) { | ||
undefined(function() { | ||
return base64; | ||
}); | ||
} else if (freeExports && !freeExports.nodeType) { | ||
if (freeModule) { // in Node.js or RingoJS v0.8.0+ | ||
freeModule.exports = base64; | ||
} else { // in Narwhal or RingoJS v0.7.0- | ||
for (var key in base64) { | ||
base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); | ||
} | ||
} | ||
} else { // in Rhino or a web browser | ||
root.base64 = base64; | ||
} | ||
}(commonjsGlobal)); | ||
}); | ||
/* | ||
@@ -96,2 +273,5 @@ * Copyright 2017 The Nakama Authors | ||
onnotification(notification) {} | ||
onmatchmakematched(match) {} | ||
onmatchdata(matchdata) {} | ||
onmatchpresence(presenceUpdate) {} | ||
@@ -126,10 +306,6 @@ login(request) { | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'wss' : 'ws'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.search = searchParams.toString(); | ||
url.pathname = '/api'; | ||
const protocol = this.ssl ? 'wss' : 'ws'; | ||
const url = `${protocol}://${this.host}:${this.port}/api?format=json&lang=${this.lang}&token=${session.token_}`; | ||
this.socket_ = new WebSocket(url.toString()); | ||
this.socket_ = new WebSocket(url); | ||
this.socket_.onclose = event => { | ||
@@ -161,2 +337,9 @@ this.ondisconnect(event); | ||
}); | ||
} else if (message.matchmakeMatched) { | ||
this.onmatchmakematched(message.matchmakeMatched); | ||
} else if (message.matchData) { | ||
message.matchData.data = JSON.parse(base64.decode(message.matchData.data)); | ||
this.onmatchdata(message.matchData); | ||
} else if (message.matchPresence) { | ||
this.onmatchpresence(message.matchPresence); | ||
} else { | ||
@@ -227,3 +410,3 @@ if (window.console) { | ||
message.rpc.payload = message.rpc.payload ? JSON.parse(message.rpc.payload) : null; | ||
p.resolve({ id: message.rcp.id, payload: message.rpc.payload }); | ||
p.resolve({ id: message.rpc.id, payload: message.rpc.payload }); | ||
} else if (message.notifications) { | ||
@@ -235,2 +418,8 @@ message.notifications.notifications.forEach(function (notification) { | ||
p.resolve(message.notifications); | ||
} else if (message.matchmakeTicket) { | ||
p.resolve(message.matchmakeTicket); | ||
} else if (message.match) { | ||
p.resolve(message.match); | ||
} else if (message.matches) { | ||
p.resolve(message.matches); | ||
} else { | ||
@@ -294,14 +483,12 @@ // if the object has properties, other than the collationId, log a warning | ||
message["collationId"] = uuidv4(); | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'https' : 'http'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.pathname = path; | ||
const protocol = this.ssl ? 'https' : 'http'; | ||
const url = `${protocol}://${this.host}:${this.port}${path}`; | ||
if (this.verbose && window.console) { | ||
console.log("AuthenticateRequest: %s, %o", url.toString(), message); | ||
console.log("AuthenticateRequest: %s, %o", url, message); | ||
} | ||
var verbose = this.verbose; | ||
return fetch(url.toString(), { | ||
return fetch(url, { | ||
"method": "POST", | ||
@@ -311,3 +498,3 @@ "body": JSON.stringify(message), | ||
"Accept-Language": this.lang, | ||
"Authorization": 'Basic ' + btoa(this.serverKey + ':'), | ||
"Authorization": 'Basic ' + base64.encode(this.serverKey + ':'), | ||
"Content-Type": 'application/json', | ||
@@ -360,3 +547,3 @@ "Accept": 'application/json', | ||
} | ||
const decoded = JSON.parse(atob(parts[1])); | ||
const decoded = JSON.parse(base64.decode(parts[1])); | ||
const expiresAt = Math.floor(parseInt(decoded['exp']) * 1000); | ||
@@ -1082,2 +1269,151 @@ | ||
class MatchmakeAddRequest { | ||
constructor(requiredCount) { | ||
this.requiredCount = requiredCount; | ||
this.filters = []; | ||
this.properties = []; | ||
} | ||
addTermFilter(name, termSet, matchAllTerms) { | ||
this.filters.push({ | ||
name: name, | ||
term: { | ||
terms: termSet, | ||
matchAllTerms: matchAllTerms | ||
} | ||
}); | ||
return this; | ||
} | ||
addRangeFilter(name, lowerbound, upperbound) { | ||
this.filters.push({ | ||
name: name, | ||
range: { | ||
lowerBound: lowerbound, | ||
upperBound: upperbound | ||
} | ||
}); | ||
return this; | ||
} | ||
addCheckFilter(name, value) { | ||
this.filters.push({ | ||
name: name, | ||
check: value | ||
}); | ||
return this; | ||
} | ||
addStringSetProperty(key, termSet) { | ||
this.properties.push({ | ||
key: key, | ||
stringSet: termSet | ||
}); | ||
return this; | ||
} | ||
addIntegerProperty(key, integerValue) { | ||
this.properties.push({ | ||
key: key, | ||
intValue: integerValue | ||
}); | ||
return this; | ||
} | ||
addBooleanProperty(key, boolValue) { | ||
this.properties.push({ | ||
key: key, | ||
boolValue: boolValue | ||
}); | ||
return this; | ||
} | ||
build_() { | ||
return { | ||
matchmakeAdd: { | ||
requiredCount: this.requiredCount, | ||
filters: this.filters, | ||
properties: this.properties | ||
} | ||
}; | ||
} | ||
} | ||
class MatchmakeRemoveRequest { | ||
constructor(ticket) { | ||
this.ticket = ticket; | ||
} | ||
build_() { | ||
return { matchmakeRemove: { ticket: this.ticket } }; | ||
} | ||
} | ||
class MatchCreateRequest { | ||
constructor() {} | ||
build_() { | ||
return { | ||
matchCreate: {} | ||
}; | ||
} | ||
} | ||
class MatchesJoinRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
this.tokens = []; | ||
} | ||
build_() { | ||
var msg = { | ||
matchesJoin: { matches: [] } | ||
}; | ||
this.matchIds.forEach(function (id) { | ||
msg.matchesJoin.matches.push({ matchId: id }); | ||
}); | ||
this.tokens.forEach(function (token) { | ||
msg.matchesJoin.matches.push({ token: token }); | ||
}); | ||
return msg; | ||
} | ||
} | ||
class MatchesLeaveRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
} | ||
build_() { | ||
return { | ||
matchesLeave: { | ||
matchIds: this.matchIds | ||
} | ||
}; | ||
} | ||
} | ||
class MatchDataSendRequest { | ||
constructor() { | ||
this.matchId = null; | ||
this.presence = null; //UserPresence | ||
this.opCode = 0; | ||
this.data = null; | ||
} | ||
build_() { | ||
return { | ||
matchDataSend: { | ||
matchId: matchId, | ||
presence: presence, | ||
opCode: opCode, | ||
data: base64.encode(JSON.stringify(data)) | ||
} | ||
}; | ||
} | ||
} | ||
/* | ||
@@ -1139,1 +1475,7 @@ * Copyright 2017 The Nakama Authors | ||
exports.RpcRequest = RpcRequest; | ||
exports.MatchmakeAddRequest = MatchmakeAddRequest; | ||
exports.MatchmakeRemoveRequest = MatchmakeRemoveRequest; | ||
exports.MatchCreateRequest = MatchCreateRequest; | ||
exports.MatchesJoinRequest = MatchesJoinRequest; | ||
exports.MatchesLeaveRequest = MatchesLeaveRequest; | ||
exports.MatchDataSendRequest = MatchDataSendRequest; |
@@ -0,1 +1,178 @@ | ||
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var base64 = createCommonjsModule(function (module, exports) { | ||
/*! http://mths.be/base64 v0.1.0 by @mathias | MIT license */ | ||
(function(root) { | ||
// Detect free variables `exports`. | ||
var freeExports = 'object' == 'object' && exports; | ||
// Detect free variable `module`. | ||
var freeModule = 'object' == 'object' && module && | ||
module.exports == freeExports && module; | ||
// Detect free variable `global`, from Node.js or Browserified code, and use | ||
// it as `root`. | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal; | ||
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { | ||
root = freeGlobal; | ||
} | ||
/*--------------------------------------------------------------------------*/ | ||
var InvalidCharacterError = function(message) { | ||
this.message = message; | ||
}; | ||
InvalidCharacterError.prototype = new Error; | ||
InvalidCharacterError.prototype.name = 'InvalidCharacterError'; | ||
var error = function(message) { | ||
// Note: the error messages used throughout this file match those used by | ||
// the native `atob`/`btoa` implementation in Chromium. | ||
throw new InvalidCharacterError(message); | ||
}; | ||
var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// http://whatwg.org/html/common-microsyntaxes.html#space-character | ||
var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g; | ||
// `decode` is designed to be fully compatible with `atob` as described in the | ||
// HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob | ||
// The optimized base64-decoding algorithm used is based on @atk’s excellent | ||
// implementation. https://gist.github.com/atk/1020396 | ||
var decode = function(input) { | ||
input = String(input) | ||
.replace(REGEX_SPACE_CHARACTERS, ''); | ||
var length = input.length; | ||
if (length % 4 == 0) { | ||
input = input.replace(/==?$/, ''); | ||
length = input.length; | ||
} | ||
if ( | ||
length % 4 == 1 || | ||
// http://whatwg.org/C#alphanumeric-ascii-characters | ||
/[^+a-zA-Z0-9/]/.test(input) | ||
) { | ||
error( | ||
'Invalid character: the string to be decoded is not correctly encoded.' | ||
); | ||
} | ||
var bitCounter = 0; | ||
var bitStorage; | ||
var buffer; | ||
var output = ''; | ||
var position = -1; | ||
while (++position < length) { | ||
buffer = TABLE.indexOf(input.charAt(position)); | ||
bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; | ||
// Unless this is the first of a group of 4 characters… | ||
if (bitCounter++ % 4) { | ||
// …convert the first 8 bits to a single ASCII character. | ||
output += String.fromCharCode( | ||
0xFF & bitStorage >> (-2 * bitCounter & 6) | ||
); | ||
} | ||
} | ||
return output; | ||
}; | ||
// `encode` is designed to be fully compatible with `btoa` as described in the | ||
// HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa | ||
var encode = function(input) { | ||
input = String(input); | ||
if (/[^\0-\xFF]/.test(input)) { | ||
// Note: no need to special-case astral symbols here, as surrogates are | ||
// matched, and the input is supposed to only contain ASCII anyway. | ||
error( | ||
'The string to be encoded contains characters outside of the ' + | ||
'Latin1 range.' | ||
); | ||
} | ||
var padding = input.length % 3; | ||
var output = ''; | ||
var position = -1; | ||
var a; | ||
var b; | ||
var c; | ||
var buffer; | ||
// Make sure any padding is handled outside of the loop. | ||
var length = input.length - padding; | ||
while (++position < length) { | ||
// Read three bytes, i.e. 24 bits. | ||
a = input.charCodeAt(position) << 16; | ||
b = input.charCodeAt(++position) << 8; | ||
c = input.charCodeAt(++position); | ||
buffer = a + b + c; | ||
// Turn the 24 bits into four chunks of 6 bits each, and append the | ||
// matching character for each of them to the output. | ||
output += ( | ||
TABLE.charAt(buffer >> 18 & 0x3F) + | ||
TABLE.charAt(buffer >> 12 & 0x3F) + | ||
TABLE.charAt(buffer >> 6 & 0x3F) + | ||
TABLE.charAt(buffer & 0x3F) | ||
); | ||
} | ||
if (padding == 2) { | ||
a = input.charCodeAt(position) << 8; | ||
b = input.charCodeAt(++position); | ||
buffer = a + b; | ||
output += ( | ||
TABLE.charAt(buffer >> 10) + | ||
TABLE.charAt((buffer >> 4) & 0x3F) + | ||
TABLE.charAt((buffer << 2) & 0x3F) + | ||
'=' | ||
); | ||
} else if (padding == 1) { | ||
buffer = input.charCodeAt(position); | ||
output += ( | ||
TABLE.charAt(buffer >> 2) + | ||
TABLE.charAt((buffer << 4) & 0x3F) + | ||
'==' | ||
); | ||
} | ||
return output; | ||
}; | ||
var base64 = { | ||
'encode': encode, | ||
'decode': decode, | ||
'version': '0.1.0' | ||
}; | ||
// Some AMD build optimizers, like r.js, check for specific condition patterns | ||
// like the following: | ||
if ( | ||
typeof undefined == 'function' && | ||
typeof undefined.amd == 'object' && | ||
undefined.amd | ||
) { | ||
undefined(function() { | ||
return base64; | ||
}); | ||
} else if (freeExports && !freeExports.nodeType) { | ||
if (freeModule) { // in Node.js or RingoJS v0.8.0+ | ||
freeModule.exports = base64; | ||
} else { // in Narwhal or RingoJS v0.7.0- | ||
for (var key in base64) { | ||
base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); | ||
} | ||
} | ||
} else { // in Rhino or a web browser | ||
root.base64 = base64; | ||
} | ||
}(commonjsGlobal)); | ||
}); | ||
/* | ||
@@ -91,2 +268,5 @@ * Copyright 2017 The Nakama Authors | ||
onnotification(notification) {} | ||
onmatchmakematched(match) {} | ||
onmatchdata(matchdata) {} | ||
onmatchpresence(presenceUpdate) {} | ||
@@ -121,10 +301,6 @@ login(request) { | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'wss' : 'ws'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.search = searchParams.toString(); | ||
url.pathname = '/api'; | ||
const protocol = this.ssl ? 'wss' : 'ws'; | ||
const url = `${protocol}://${this.host}:${this.port}/api?format=json&lang=${this.lang}&token=${session.token_}`; | ||
this.socket_ = new WebSocket(url.toString()); | ||
this.socket_ = new WebSocket(url); | ||
this.socket_.onclose = event => { | ||
@@ -156,2 +332,9 @@ this.ondisconnect(event); | ||
}); | ||
} else if (message.matchmakeMatched) { | ||
this.onmatchmakematched(message.matchmakeMatched); | ||
} else if (message.matchData) { | ||
message.matchData.data = JSON.parse(base64.decode(message.matchData.data)); | ||
this.onmatchdata(message.matchData); | ||
} else if (message.matchPresence) { | ||
this.onmatchpresence(message.matchPresence); | ||
} else { | ||
@@ -222,3 +405,3 @@ if (window.console) { | ||
message.rpc.payload = message.rpc.payload ? JSON.parse(message.rpc.payload) : null; | ||
p.resolve({ id: message.rcp.id, payload: message.rpc.payload }); | ||
p.resolve({ id: message.rpc.id, payload: message.rpc.payload }); | ||
} else if (message.notifications) { | ||
@@ -230,2 +413,8 @@ message.notifications.notifications.forEach(function (notification) { | ||
p.resolve(message.notifications); | ||
} else if (message.matchmakeTicket) { | ||
p.resolve(message.matchmakeTicket); | ||
} else if (message.match) { | ||
p.resolve(message.match); | ||
} else if (message.matches) { | ||
p.resolve(message.matches); | ||
} else { | ||
@@ -289,14 +478,12 @@ // if the object has properties, other than the collationId, log a warning | ||
message["collationId"] = uuidv4(); | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'https' : 'http'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.pathname = path; | ||
const protocol = this.ssl ? 'https' : 'http'; | ||
const url = `${protocol}://${this.host}:${this.port}${path}`; | ||
if (this.verbose && window.console) { | ||
console.log("AuthenticateRequest: %s, %o", url.toString(), message); | ||
console.log("AuthenticateRequest: %s, %o", url, message); | ||
} | ||
var verbose = this.verbose; | ||
return fetch(url.toString(), { | ||
return fetch(url, { | ||
"method": "POST", | ||
@@ -306,3 +493,3 @@ "body": JSON.stringify(message), | ||
"Accept-Language": this.lang, | ||
"Authorization": 'Basic ' + btoa(this.serverKey + ':'), | ||
"Authorization": 'Basic ' + base64.encode(this.serverKey + ':'), | ||
"Content-Type": 'application/json', | ||
@@ -355,3 +542,3 @@ "Accept": 'application/json', | ||
} | ||
const decoded = JSON.parse(atob(parts[1])); | ||
const decoded = JSON.parse(base64.decode(parts[1])); | ||
const expiresAt = Math.floor(parseInt(decoded['exp']) * 1000); | ||
@@ -1077,2 +1264,151 @@ | ||
class MatchmakeAddRequest { | ||
constructor(requiredCount) { | ||
this.requiredCount = requiredCount; | ||
this.filters = []; | ||
this.properties = []; | ||
} | ||
addTermFilter(name, termSet, matchAllTerms) { | ||
this.filters.push({ | ||
name: name, | ||
term: { | ||
terms: termSet, | ||
matchAllTerms: matchAllTerms | ||
} | ||
}); | ||
return this; | ||
} | ||
addRangeFilter(name, lowerbound, upperbound) { | ||
this.filters.push({ | ||
name: name, | ||
range: { | ||
lowerBound: lowerbound, | ||
upperBound: upperbound | ||
} | ||
}); | ||
return this; | ||
} | ||
addCheckFilter(name, value) { | ||
this.filters.push({ | ||
name: name, | ||
check: value | ||
}); | ||
return this; | ||
} | ||
addStringSetProperty(key, termSet) { | ||
this.properties.push({ | ||
key: key, | ||
stringSet: termSet | ||
}); | ||
return this; | ||
} | ||
addIntegerProperty(key, integerValue) { | ||
this.properties.push({ | ||
key: key, | ||
intValue: integerValue | ||
}); | ||
return this; | ||
} | ||
addBooleanProperty(key, boolValue) { | ||
this.properties.push({ | ||
key: key, | ||
boolValue: boolValue | ||
}); | ||
return this; | ||
} | ||
build_() { | ||
return { | ||
matchmakeAdd: { | ||
requiredCount: this.requiredCount, | ||
filters: this.filters, | ||
properties: this.properties | ||
} | ||
}; | ||
} | ||
} | ||
class MatchmakeRemoveRequest { | ||
constructor(ticket) { | ||
this.ticket = ticket; | ||
} | ||
build_() { | ||
return { matchmakeRemove: { ticket: this.ticket } }; | ||
} | ||
} | ||
class MatchCreateRequest { | ||
constructor() {} | ||
build_() { | ||
return { | ||
matchCreate: {} | ||
}; | ||
} | ||
} | ||
class MatchesJoinRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
this.tokens = []; | ||
} | ||
build_() { | ||
var msg = { | ||
matchesJoin: { matches: [] } | ||
}; | ||
this.matchIds.forEach(function (id) { | ||
msg.matchesJoin.matches.push({ matchId: id }); | ||
}); | ||
this.tokens.forEach(function (token) { | ||
msg.matchesJoin.matches.push({ token: token }); | ||
}); | ||
return msg; | ||
} | ||
} | ||
class MatchesLeaveRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
} | ||
build_() { | ||
return { | ||
matchesLeave: { | ||
matchIds: this.matchIds | ||
} | ||
}; | ||
} | ||
} | ||
class MatchDataSendRequest { | ||
constructor() { | ||
this.matchId = null; | ||
this.presence = null; //UserPresence | ||
this.opCode = 0; | ||
this.data = null; | ||
} | ||
build_() { | ||
return { | ||
matchDataSend: { | ||
matchId: matchId, | ||
presence: presence, | ||
opCode: opCode, | ||
data: base64.encode(JSON.stringify(data)) | ||
} | ||
}; | ||
} | ||
} | ||
/* | ||
@@ -1098,2 +1434,2 @@ * Copyright 2017 The Nakama Authors | ||
export { AuthenticateRequest, Client, Session, LinkRequest, UnlinkRequest, SelfFetchRequest, SelfUpdateRequest, UsersFetchRequest, StorageListRequest, StorageFetchRequest, StorageWriteRequest, StorageRemoveRequest, StorageUpdateRequest, FriendsAddRequest, FriendsRemoveRequest, FriendsBlockRequest, FriendsListRequest, TopicsJoinRequest, TopicsLeaveRequest, TopicMessageSendRequest, TopicMessagesListRequest, GroupsCreateRequest, GroupsUpdateRequest, GroupsRemoveRequest, GroupsFetchRequest, GroupsListRequest, GroupsSelfListRequest, GroupUsersListRequest, GroupsJoinRequest, GroupsLeaveRequest, GroupUsersAddRequest, GroupUsersKickRequest, GroupUsersPromoteRequest, NotificationsListRequest, NotificationsRemoveRequest, RpcRequest }; | ||
export { AuthenticateRequest, Client, Session, LinkRequest, UnlinkRequest, SelfFetchRequest, SelfUpdateRequest, UsersFetchRequest, StorageListRequest, StorageFetchRequest, StorageWriteRequest, StorageRemoveRequest, StorageUpdateRequest, FriendsAddRequest, FriendsRemoveRequest, FriendsBlockRequest, FriendsListRequest, TopicsJoinRequest, TopicsLeaveRequest, TopicMessageSendRequest, TopicMessagesListRequest, GroupsCreateRequest, GroupsUpdateRequest, GroupsRemoveRequest, GroupsFetchRequest, GroupsListRequest, GroupsSelfListRequest, GroupUsersListRequest, GroupsJoinRequest, GroupsLeaveRequest, GroupUsersAddRequest, GroupUsersKickRequest, GroupUsersPromoteRequest, NotificationsListRequest, NotificationsRemoveRequest, RpcRequest, MatchmakeAddRequest, MatchmakeRemoveRequest, MatchCreateRequest, MatchesJoinRequest, MatchesLeaveRequest, MatchDataSendRequest }; |
@@ -7,2 +7,179 @@ (function (global, factory) { | ||
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var base64 = createCommonjsModule(function (module, exports) { | ||
/*! http://mths.be/base64 v0.1.0 by @mathias | MIT license */ | ||
(function(root) { | ||
// Detect free variables `exports`. | ||
var freeExports = 'object' == 'object' && exports; | ||
// Detect free variable `module`. | ||
var freeModule = 'object' == 'object' && module && | ||
module.exports == freeExports && module; | ||
// Detect free variable `global`, from Node.js or Browserified code, and use | ||
// it as `root`. | ||
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal; | ||
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { | ||
root = freeGlobal; | ||
} | ||
/*--------------------------------------------------------------------------*/ | ||
var InvalidCharacterError = function(message) { | ||
this.message = message; | ||
}; | ||
InvalidCharacterError.prototype = new Error; | ||
InvalidCharacterError.prototype.name = 'InvalidCharacterError'; | ||
var error = function(message) { | ||
// Note: the error messages used throughout this file match those used by | ||
// the native `atob`/`btoa` implementation in Chromium. | ||
throw new InvalidCharacterError(message); | ||
}; | ||
var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// http://whatwg.org/html/common-microsyntaxes.html#space-character | ||
var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g; | ||
// `decode` is designed to be fully compatible with `atob` as described in the | ||
// HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob | ||
// The optimized base64-decoding algorithm used is based on @atk’s excellent | ||
// implementation. https://gist.github.com/atk/1020396 | ||
var decode = function(input) { | ||
input = String(input) | ||
.replace(REGEX_SPACE_CHARACTERS, ''); | ||
var length = input.length; | ||
if (length % 4 == 0) { | ||
input = input.replace(/==?$/, ''); | ||
length = input.length; | ||
} | ||
if ( | ||
length % 4 == 1 || | ||
// http://whatwg.org/C#alphanumeric-ascii-characters | ||
/[^+a-zA-Z0-9/]/.test(input) | ||
) { | ||
error( | ||
'Invalid character: the string to be decoded is not correctly encoded.' | ||
); | ||
} | ||
var bitCounter = 0; | ||
var bitStorage; | ||
var buffer; | ||
var output = ''; | ||
var position = -1; | ||
while (++position < length) { | ||
buffer = TABLE.indexOf(input.charAt(position)); | ||
bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; | ||
// Unless this is the first of a group of 4 characters… | ||
if (bitCounter++ % 4) { | ||
// …convert the first 8 bits to a single ASCII character. | ||
output += String.fromCharCode( | ||
0xFF & bitStorage >> (-2 * bitCounter & 6) | ||
); | ||
} | ||
} | ||
return output; | ||
}; | ||
// `encode` is designed to be fully compatible with `btoa` as described in the | ||
// HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa | ||
var encode = function(input) { | ||
input = String(input); | ||
if (/[^\0-\xFF]/.test(input)) { | ||
// Note: no need to special-case astral symbols here, as surrogates are | ||
// matched, and the input is supposed to only contain ASCII anyway. | ||
error( | ||
'The string to be encoded contains characters outside of the ' + | ||
'Latin1 range.' | ||
); | ||
} | ||
var padding = input.length % 3; | ||
var output = ''; | ||
var position = -1; | ||
var a; | ||
var b; | ||
var c; | ||
var buffer; | ||
// Make sure any padding is handled outside of the loop. | ||
var length = input.length - padding; | ||
while (++position < length) { | ||
// Read three bytes, i.e. 24 bits. | ||
a = input.charCodeAt(position) << 16; | ||
b = input.charCodeAt(++position) << 8; | ||
c = input.charCodeAt(++position); | ||
buffer = a + b + c; | ||
// Turn the 24 bits into four chunks of 6 bits each, and append the | ||
// matching character for each of them to the output. | ||
output += ( | ||
TABLE.charAt(buffer >> 18 & 0x3F) + | ||
TABLE.charAt(buffer >> 12 & 0x3F) + | ||
TABLE.charAt(buffer >> 6 & 0x3F) + | ||
TABLE.charAt(buffer & 0x3F) | ||
); | ||
} | ||
if (padding == 2) { | ||
a = input.charCodeAt(position) << 8; | ||
b = input.charCodeAt(++position); | ||
buffer = a + b; | ||
output += ( | ||
TABLE.charAt(buffer >> 10) + | ||
TABLE.charAt((buffer >> 4) & 0x3F) + | ||
TABLE.charAt((buffer << 2) & 0x3F) + | ||
'=' | ||
); | ||
} else if (padding == 1) { | ||
buffer = input.charCodeAt(position); | ||
output += ( | ||
TABLE.charAt(buffer >> 2) + | ||
TABLE.charAt((buffer << 4) & 0x3F) + | ||
'==' | ||
); | ||
} | ||
return output; | ||
}; | ||
var base64 = { | ||
'encode': encode, | ||
'decode': decode, | ||
'version': '0.1.0' | ||
}; | ||
// Some AMD build optimizers, like r.js, check for specific condition patterns | ||
// like the following: | ||
if ( | ||
typeof undefined == 'function' && | ||
typeof undefined.amd == 'object' && | ||
undefined.amd | ||
) { | ||
undefined(function() { | ||
return base64; | ||
}); | ||
} else if (freeExports && !freeExports.nodeType) { | ||
if (freeModule) { // in Node.js or RingoJS v0.8.0+ | ||
freeModule.exports = base64; | ||
} else { // in Narwhal or RingoJS v0.7.0- | ||
for (var key in base64) { | ||
base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); | ||
} | ||
} | ||
} else { // in Rhino or a web browser | ||
root.base64 = base64; | ||
} | ||
}(commonjsGlobal)); | ||
}); | ||
/* | ||
@@ -98,2 +275,5 @@ * Copyright 2017 The Nakama Authors | ||
onnotification(notification) {} | ||
onmatchmakematched(match) {} | ||
onmatchdata(matchdata) {} | ||
onmatchpresence(presenceUpdate) {} | ||
@@ -128,10 +308,6 @@ login(request) { | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'wss' : 'ws'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.search = searchParams.toString(); | ||
url.pathname = '/api'; | ||
const protocol = this.ssl ? 'wss' : 'ws'; | ||
const url = `${protocol}://${this.host}:${this.port}/api?format=json&lang=${this.lang}&token=${session.token_}`; | ||
this.socket_ = new WebSocket(url.toString()); | ||
this.socket_ = new WebSocket(url); | ||
this.socket_.onclose = event => { | ||
@@ -163,2 +339,9 @@ this.ondisconnect(event); | ||
}); | ||
} else if (message.matchmakeMatched) { | ||
this.onmatchmakematched(message.matchmakeMatched); | ||
} else if (message.matchData) { | ||
message.matchData.data = JSON.parse(base64.decode(message.matchData.data)); | ||
this.onmatchdata(message.matchData); | ||
} else if (message.matchPresence) { | ||
this.onmatchpresence(message.matchPresence); | ||
} else { | ||
@@ -229,3 +412,3 @@ if (window.console) { | ||
message.rpc.payload = message.rpc.payload ? JSON.parse(message.rpc.payload) : null; | ||
p.resolve({ id: message.rcp.id, payload: message.rpc.payload }); | ||
p.resolve({ id: message.rpc.id, payload: message.rpc.payload }); | ||
} else if (message.notifications) { | ||
@@ -237,2 +420,8 @@ message.notifications.notifications.forEach(function (notification) { | ||
p.resolve(message.notifications); | ||
} else if (message.matchmakeTicket) { | ||
p.resolve(message.matchmakeTicket); | ||
} else if (message.match) { | ||
p.resolve(message.match); | ||
} else if (message.matches) { | ||
p.resolve(message.matches); | ||
} else { | ||
@@ -296,14 +485,12 @@ // if the object has properties, other than the collationId, log a warning | ||
message["collationId"] = uuidv4(); | ||
const url = new URL(window.location.href); | ||
url.protocol = this.ssl ? 'https' : 'http'; | ||
url.hostname = this.host; | ||
url.port = this.port; | ||
url.pathname = path; | ||
const protocol = this.ssl ? 'https' : 'http'; | ||
const url = `${protocol}://${this.host}:${this.port}${path}`; | ||
if (this.verbose && window.console) { | ||
console.log("AuthenticateRequest: %s, %o", url.toString(), message); | ||
console.log("AuthenticateRequest: %s, %o", url, message); | ||
} | ||
var verbose = this.verbose; | ||
return fetch(url.toString(), { | ||
return fetch(url, { | ||
"method": "POST", | ||
@@ -313,3 +500,3 @@ "body": JSON.stringify(message), | ||
"Accept-Language": this.lang, | ||
"Authorization": 'Basic ' + btoa(this.serverKey + ':'), | ||
"Authorization": 'Basic ' + base64.encode(this.serverKey + ':'), | ||
"Content-Type": 'application/json', | ||
@@ -362,3 +549,3 @@ "Accept": 'application/json', | ||
} | ||
const decoded = JSON.parse(atob(parts[1])); | ||
const decoded = JSON.parse(base64.decode(parts[1])); | ||
const expiresAt = Math.floor(parseInt(decoded['exp']) * 1000); | ||
@@ -1084,2 +1271,151 @@ | ||
class MatchmakeAddRequest { | ||
constructor(requiredCount) { | ||
this.requiredCount = requiredCount; | ||
this.filters = []; | ||
this.properties = []; | ||
} | ||
addTermFilter(name, termSet, matchAllTerms) { | ||
this.filters.push({ | ||
name: name, | ||
term: { | ||
terms: termSet, | ||
matchAllTerms: matchAllTerms | ||
} | ||
}); | ||
return this; | ||
} | ||
addRangeFilter(name, lowerbound, upperbound) { | ||
this.filters.push({ | ||
name: name, | ||
range: { | ||
lowerBound: lowerbound, | ||
upperBound: upperbound | ||
} | ||
}); | ||
return this; | ||
} | ||
addCheckFilter(name, value) { | ||
this.filters.push({ | ||
name: name, | ||
check: value | ||
}); | ||
return this; | ||
} | ||
addStringSetProperty(key, termSet) { | ||
this.properties.push({ | ||
key: key, | ||
stringSet: termSet | ||
}); | ||
return this; | ||
} | ||
addIntegerProperty(key, integerValue) { | ||
this.properties.push({ | ||
key: key, | ||
intValue: integerValue | ||
}); | ||
return this; | ||
} | ||
addBooleanProperty(key, boolValue) { | ||
this.properties.push({ | ||
key: key, | ||
boolValue: boolValue | ||
}); | ||
return this; | ||
} | ||
build_() { | ||
return { | ||
matchmakeAdd: { | ||
requiredCount: this.requiredCount, | ||
filters: this.filters, | ||
properties: this.properties | ||
} | ||
}; | ||
} | ||
} | ||
class MatchmakeRemoveRequest { | ||
constructor(ticket) { | ||
this.ticket = ticket; | ||
} | ||
build_() { | ||
return { matchmakeRemove: { ticket: this.ticket } }; | ||
} | ||
} | ||
class MatchCreateRequest { | ||
constructor() {} | ||
build_() { | ||
return { | ||
matchCreate: {} | ||
}; | ||
} | ||
} | ||
class MatchesJoinRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
this.tokens = []; | ||
} | ||
build_() { | ||
var msg = { | ||
matchesJoin: { matches: [] } | ||
}; | ||
this.matchIds.forEach(function (id) { | ||
msg.matchesJoin.matches.push({ matchId: id }); | ||
}); | ||
this.tokens.forEach(function (token) { | ||
msg.matchesJoin.matches.push({ token: token }); | ||
}); | ||
return msg; | ||
} | ||
} | ||
class MatchesLeaveRequest { | ||
constructor() { | ||
this.matchIds = []; | ||
} | ||
build_() { | ||
return { | ||
matchesLeave: { | ||
matchIds: this.matchIds | ||
} | ||
}; | ||
} | ||
} | ||
class MatchDataSendRequest { | ||
constructor() { | ||
this.matchId = null; | ||
this.presence = null; //UserPresence | ||
this.opCode = 0; | ||
this.data = null; | ||
} | ||
build_() { | ||
return { | ||
matchDataSend: { | ||
matchId: matchId, | ||
presence: presence, | ||
opCode: opCode, | ||
data: base64.encode(JSON.stringify(data)) | ||
} | ||
}; | ||
} | ||
} | ||
/* | ||
@@ -1141,2 +1477,8 @@ * Copyright 2017 The Nakama Authors | ||
exports.RpcRequest = RpcRequest; | ||
exports.MatchmakeAddRequest = MatchmakeAddRequest; | ||
exports.MatchmakeRemoveRequest = MatchmakeRemoveRequest; | ||
exports.MatchCreateRequest = MatchCreateRequest; | ||
exports.MatchesJoinRequest = MatchesJoinRequest; | ||
exports.MatchesLeaveRequest = MatchesLeaveRequest; | ||
exports.MatchDataSendRequest = MatchDataSendRequest; | ||
@@ -1143,0 +1485,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "@heroiclabs/nakama-js", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "JavaScript client for Nakama server.", | ||
@@ -22,2 +22,5 @@ "keywords": ["realtime", "realtime chat", "nakama", "game server"], | ||
"private": false, | ||
"dependencies": { | ||
"base-64": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -24,0 +27,0 @@ "babel-core": "^6.26.0", |
@@ -12,3 +12,3 @@ Nakama JS | ||
You can add the client to your project with `yarn add nakama-js`. This will add the dependency to your `package.json`. You can also use NPM or Bower to download and add the dependency to your project. | ||
You can add the client to your project with `yarn add @heroiclabs/nakama-js`. This will add the dependency to your `package.json`. You can also use NPM or Bower to download and add the dependency to your project. | ||
@@ -53,3 +53,3 @@ We have a guide which covers how to use the client with lots of code examples: | ||
```shell | ||
$> yarn run build && yarn publish | ||
$> yarn run build && yarn publish --access=public | ||
``` |
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
107826
6
3802
1
+ Addedbase-64@^0.1.0
+ Addedbase-64@0.1.0(transitive)