function APNCertificate(cert) {
if(!cert.publicKey || !cert.validity || !cert.subject) {
throw new Error("certificate object is invalid");
}
this._cert = cert;
}n/a
function APNKey(key) {
if(!key || !key.n || !key.e) {
throw new Error("key is not a valid public key");
}
this._key = key;
}n/a
function Notification(payload) {
this.encoding = "utf8";
this.payload = {};
this.compiled = false;
this.aps = {};
this.expiry = 0;
this.priority = 10;
if (payload) {
for(let key in payload) {
if (payload.hasOwnProperty(key)) {
this[key] = payload[key];
}
}
}
}n/a
function Provider(options) {
if(false === (this instanceof Provider)) {
return new Provider(options);
}
this.client = new Client(options);
EventEmitter.call(this);
}n/a
function token(input) {
let token;
if (typeof input === "string") {
token = input;
} else if (Buffer.isBuffer(input)) {
token = input.toString("hex");
}
token = token.replace(/[^0-9a-f]/gi, "");
if (token.length === 0) {
throw new Error("Token has invalid length");
}
return token;
}n/a
function APNCertificate(cert) {
if(!cert.publicKey || !cert.validity || !cert.subject) {
throw new Error("certificate object is invalid");
}
this._cert = cert;
}n/a
environment = function () {
let environment = { sandbox: false, production: false };
if (this._cert.getExtension({ "id": oids.applePushServiceClientDevelopment })) {
environment.sandbox = true;
}
if (this._cert.getExtension({ "id": oids.applePushServiceClientProduction })) {
environment.production = true;
}
return environment;
}...
let validity = certificate.validity();
if (validity.notAfter.getTime() < Date.now()) {
throw new Error("certificate has expired: " + validity.notAfter.toJSON());
}
if (credentials.production !== undefined) {
let environment = certificate.environment();
if ( (credentials.production && !environment.production) ||
(!credentials.production && !environment.sandbox)) {
throw new Error("certificate does not support configured environment, production: " + credentials.production);
}
}
}
...key = function () {
return new APNKey(this._cert.publicKey);
}...
"use strict";
function validateCredentials(credentials) {
let certificate = credentials.certificates[0];
if (credentials.key.fingerprint() !== certificate.key().fingerprint()) {
throw new Error("certificate and key do not match");
}
let validity = certificate.validity();
if (validity.notAfter.getTime() < Date.now()) {
throw new Error("certificate has expired: " + validity.notAfter.toJSON());
}
...validity = function () {
return this._cert.validity;
}...
function validateCredentials(credentials) {
let certificate = credentials.certificates[0];
if (credentials.key.fingerprint() !== certificate.key().fingerprint()) {
throw new Error("certificate and key do not match");
}
let validity = certificate.validity();
if (validity.notAfter.getTime() < Date.now()) {
throw new Error("certificate has expired: " + validity.notAfter.toJSON());
}
if (credentials.production !== undefined) {
let environment = certificate.environment();
if ( (credentials.production && !environment.production) ||
...function APNKey(key) {
if(!key || !key.n || !key.e) {
throw new Error("key is not a valid public key");
}
this._key = key;
}n/a
fingerprint = function () {
return forge.pki.getPublicKeyFingerprint(this._key, {encoding: "hex"});
}...
"use strict";
function validateCredentials(credentials) {
let certificate = credentials.certificates[0];
if (credentials.key.fingerprint() !== certificate.key().fingerprint()) {
throw new Error("certificate and key do not match");
}
let validity = certificate.validity();
if (validity.notAfter.getTime() < Date.now()) {
throw new Error("certificate has expired: " + validity.notAfter.toJSON());
}
...function Notification(payload) {
this.encoding = "utf8";
this.payload = {};
this.compiled = false;
this.aps = {};
this.expiry = 0;
this.priority = 10;
if (payload) {
for(let key in payload) {
if (payload.hasOwnProperty(key)) {
this[key] = payload[key];
}
}
}
}n/a
apsPayload = function () {
var aps = this.aps;
return Object.keys(aps).find( key => aps[key] !== undefined ) ? aps : undefined;
}n/a
compile = function () {
if(!this.compiled) {
this.compiled = JSON.stringify(this);
}
return this.compiled;
}...
}
Provider.prototype = Object.create(EventEmitter.prototype);
Provider.prototype.send = function send(notification, recipients) {
const builtNotification = {
headers: notification.headers(),
body: notification.compile(),
};
if (!Array.isArray(recipients)) {
recipients = [recipients];
}
return Promise.all( recipients.map( token => this.client.write(builtNotification, token) ))
...function headers() {
let headers = {};
if (this.priority !== 10) {
headers["apns-priority"] = this.priority;
}
if (this.id) {
headers["apns-id"] = this.id;
}
if (this.expiry > 0) {
headers["apns-expiration"] = this.expiry;
}
if (this.topic) {
headers["apns-topic"] = this.topic;
}
if (this.collapseId) {
headers["apns-collapse-id"] = this.collapseId;
}
return headers;
}...
if (this.config.token.isExpired(3300)) {
this.config.token.regenerate(this.config.token.generation);
}
headers.authorization = "bearer " + this.config.token.current;
tokenGeneration = this.config.token.generation;
}
stream.headers(headers);
stream.write(notification.body);
return new Promise ( resolve => {
stream.on("end", () => {
if (status === "200") {
resolve({ device });
} else if (responseData !== "") {
...length = function () {
return Buffer.byteLength(this.compile(), this.encoding || "utf8");
}n/a
prepareAlert = function () {
if (typeof this.aps.alert !== "object") {
this.aps.alert = {"body": this.aps.alert};
}
}n/a
setAction = function (value) {
this[propName] = value;
return this;
}n/a
setActionLocKey = function (value) {
this[propName] = value;
return this;
}n/a
setAlert = function (value) {
this[propName] = value;
return this;
}n/a
setBadge = function (value) {
this[propName] = value;
return this;
}n/a
setBody = function (value) {
this[propName] = value;
return this;
}n/a
setCategory = function (value) {
this[propName] = value;
return this;
}n/a
setContentAvailable = function (value) {
this[propName] = value;
return this;
}n/a
setExpiry = function (value) {
this[propName] = value;
return this;
}n/a
setLaunchImage = function (value) {
this[propName] = value;
return this;
}n/a
setLocArgs = function (value) {
this[propName] = value;
return this;
}n/a
setLocKey = function (value) {
this[propName] = value;
return this;
}n/a
setMdm = function (value) {
this[propName] = value;
return this;
}n/a
setMutableContent = function (value) {
this[propName] = value;
return this;
}n/a
setPayload = function (value) {
this[propName] = value;
return this;
}n/a
setPriority = function (value) {
this[propName] = value;
return this;
}n/a
setSound = function (value) {
this[propName] = value;
return this;
}n/a
setSubtitle = function (value) {
this[propName] = value;
return this;
}n/a
setThreadId = function (value) {
this[propName] = value;
return this;
}n/a
setTitle = function (value) {
this[propName] = value;
return this;
}n/a
setTitleLocArgs = function (value) {
this[propName] = value;
return this;
}n/a
setTitleLocKey = function (value) {
this[propName] = value;
return this;
}n/a
setUrlArgs = function (value) {
this[propName] = value;
return this;
}n/a
toJSON = function () {
if (this.rawPayload != null) {
return this.rawPayload;
}
if (typeof this._mdm === "string") {
return { "mdm": this._mdm };
}
return Object.assign({}, this.payload, {aps: this.apsPayload()});
}...
if (credentials.key.fingerprint() !== certificate.key().fingerprint()) {
throw new Error("certificate and key do not match");
}
let validity = certificate.validity();
if (validity.notAfter.getTime() < Date.now()) {
throw new Error("certificate has expired: " + validity.notAfter.toJSON());
}
if (credentials.production !== undefined) {
let environment = certificate.environment();
if ( (credentials.production && !environment.production) ||
(!credentials.production && !environment.sandbox)) {
throw new Error("certificate does not support configured environment, production: " + credentials.production);
...function Provider(options) {
if(false === (this instanceof Provider)) {
return new Provider(options);
}
this.client = new Client(options);
EventEmitter.call(this);
}n/a
function send(notification, recipients) {
const builtNotification = {
headers: notification.headers(),
body: notification.compile(),
};
if (!Array.isArray(recipients)) {
recipients = [recipients];
}
return Promise.all( recipients.map( token => this.client.write(builtNotification, token) ))
.then( responses => {
let sent = [];
let failed = [];
responses.forEach( response => {
if (response.status || response.error) {
failed.push(response);
} else {
sent.push(response);
}
});
return {sent, failed};
});
}n/a
function shutdown() {
this.client.shutdown();
}...
return;
}
const resolve = this.queue.shift();
resolve(stream);
}
if (this.shutdownPending) {
this.endpointManager.shutdown();
}
});
this.endpointManager.on("error", (err) => {
this.queue.forEach((resolve) => {
resolve(Promise.reject(err));
});
...