Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@firebase/installations

Package Overview
Dependencies
Maintainers
5
Versions
2744
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@firebase/installations - npm Package Compare versions

Comparing version 0.1.7-canary.ea3adf1 to 0.1.7-canary.ed17a58

.rpt2_cache/rpt2_918cd379c126b692cff4c0d4055971d4ce2b6621/code/cache/011f65162f121c1aafb5173275d479529cef0a2e

124

dist/index.cjs.js

@@ -12,3 +12,3 @@ 'use strict';

var version = "0.1.7-canary.ea3adf1";
var version = "0.1.7-canary.ed17a58";

@@ -236,3 +236,3 @@ /**

registeredInstallationEntry = {
fid: fid,
fid: responseValue.fid || fid,
registrationStatus: 2 /* COMPLETED */,

@@ -289,4 +289,3 @@ refreshToken: responseValue.refreshToken,

*/
function bufferToBase64UrlSafe(buffer) {
var array = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
function bufferToBase64UrlSafe(array) {
var b64 = btoa(String.fromCharCode.apply(String, tslib_1.__spread(array)));

@@ -312,11 +311,24 @@ return b64.replace(/\+/g, '-').replace(/\//g, '_');

*/
/** Generates a new FID using random values from Web Crypto API. */
var VALID_FID_PATTERN = /^[cdef][\w-]{21}$/;
var INVALID_FID = '';
/**
* Generates a new FID using random values from Web Crypto API.
* Returns an empty string if FID generation fails for any reason.
*/
function generateFid() {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
var fidByteArray = new Uint8Array(17);
crypto.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 112 + (fidByteArray[0] % 16);
return encode(fidByteArray);
try {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
var fidByteArray = new Uint8Array(17);
var crypto_1 = self.crypto || self.msCrypto;
crypto_1.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 112 + (fidByteArray[0] % 16);
var fid = encode(fidByteArray);
return VALID_FID_PATTERN.test(fid) ? fid : INVALID_FID;
}
catch (_a) {
// FID generation errored
return INVALID_FID;
}
}

@@ -350,13 +362,19 @@ /** Converts a FID Uint8Array to a base64 string representation. */

var OBJECT_STORE_NAME = 'firebase-installations-store';
var dbPromise = idb.openDb(DATABASE_NAME, DATABASE_VERSION, function (upgradeDB) {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
var dbPromise = null;
function getDbPromise() {
if (!dbPromise) {
dbPromise = idb.openDb(DATABASE_NAME, DATABASE_VERSION, function (upgradeDB) {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
});
}
});
return dbPromise;
}
/** Assigns or overwrites the record for the given key with the given value. */

@@ -370,3 +388,3 @@ function set(appConfig, value) {

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -394,3 +412,3 @@ db = _a.sent();

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -423,3 +441,3 @@ db = _a.sent();

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -479,16 +497,23 @@ db = _a.sent();

return tslib_1.__awaiter(this, void 0, void 0, function () {
var registrationPromise, _a;
var registrationPromise, installationEntry, _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
case 0: return [4 /*yield*/, update(appConfig, function (oldEntry) {
var installationEntry = updateOrCreateInstallationEntry(oldEntry);
var entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
})];
case 1:
installationEntry = _b.sent();
if (!(installationEntry.fid === INVALID_FID)) return [3 /*break*/, 3];
_a = {};
return [4 /*yield*/, update(appConfig, function (oldEntry) {
var installationEntry = updateOrCreateFid(oldEntry);
var entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
})];
case 1: return [2 /*return*/, (_a.installationEntry = _b.sent(),
_a.registrationPromise = registrationPromise,
_a)];
return [4 /*yield*/, registrationPromise];
case 2:
// FID generation failed. Waiting for the FID from the server.
return [2 /*return*/, (_a.installationEntry = _b.sent(), _a)];
case 3: return [2 /*return*/, {
installationEntry: installationEntry,
registrationPromise: registrationPromise
}];
}

@@ -498,3 +523,3 @@ });

}
function updateOrCreateFid(oldEntry) {
function updateOrCreateInstallationEntry(oldEntry) {
var entry = oldEntry || {

@@ -552,22 +577,19 @@ fid: generateFid(),

case 0:
_a.trys.push([0, 3, , 8]);
_a.trys.push([0, 2, , 7]);
return [4 /*yield*/, createInstallation(appConfig, installationEntry)];
case 1:
registeredInstallationEntry = _a.sent();
return [4 /*yield*/, set(appConfig, registeredInstallationEntry)];
return [2 /*return*/, set(appConfig, registeredInstallationEntry)];
case 2:
_a.sent();
return [3 /*break*/, 8];
case 3:
e_1 = _a.sent();
if (!(isServerError(e_1) && e_1.serverCode === 409)) return [3 /*break*/, 5];
if (!(isServerError(e_1) && e_1.serverCode === 409)) return [3 /*break*/, 4];
// Server returned a "FID can not be used" error.
// Generate a new ID next time.
return [4 /*yield*/, remove(appConfig)];
case 4:
case 3:
// Server returned a "FID can not be used" error.
// Generate a new ID next time.
_a.sent();
return [3 /*break*/, 7];
case 5:
return [3 /*break*/, 6];
case 4:
// Registration failed. Set FID as not registered.

@@ -578,8 +600,8 @@ return [4 /*yield*/, set(appConfig, {

})];
case 6:
case 5:
// Registration failed. Set FID as not registered.
_a.sent();
_a.label = 7;
case 7: throw e_1;
case 8: return [2 /*return*/];
_a.label = 6;
case 6: throw e_1;
case 7: return [2 /*return*/];
}

@@ -614,3 +636,3 @@ });

}
return [2 /*return*/];
return [2 /*return*/, entry];
}

@@ -617,0 +639,0 @@ });

@@ -7,3 +7,3 @@ import firebase from '@firebase/app';

var version = "0.1.7-canary.ea3adf1";
var version = "0.1.7-canary.ed17a58";

@@ -231,3 +231,3 @@ /**

registeredInstallationEntry = {
fid: fid,
fid: responseValue.fid || fid,
registrationStatus: 2 /* COMPLETED */,

@@ -284,4 +284,3 @@ refreshToken: responseValue.refreshToken,

*/
function bufferToBase64UrlSafe(buffer) {
var array = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
function bufferToBase64UrlSafe(array) {
var b64 = btoa(String.fromCharCode.apply(String, __spread(array)));

@@ -307,11 +306,24 @@ return b64.replace(/\+/g, '-').replace(/\//g, '_');

*/
/** Generates a new FID using random values from Web Crypto API. */
var VALID_FID_PATTERN = /^[cdef][\w-]{21}$/;
var INVALID_FID = '';
/**
* Generates a new FID using random values from Web Crypto API.
* Returns an empty string if FID generation fails for any reason.
*/
function generateFid() {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
var fidByteArray = new Uint8Array(17);
crypto.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 112 + (fidByteArray[0] % 16);
return encode(fidByteArray);
try {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
var fidByteArray = new Uint8Array(17);
var crypto_1 = self.crypto || self.msCrypto;
crypto_1.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 112 + (fidByteArray[0] % 16);
var fid = encode(fidByteArray);
return VALID_FID_PATTERN.test(fid) ? fid : INVALID_FID;
}
catch (_a) {
// FID generation errored
return INVALID_FID;
}
}

@@ -345,13 +357,19 @@ /** Converts a FID Uint8Array to a base64 string representation. */

var OBJECT_STORE_NAME = 'firebase-installations-store';
var dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, function (upgradeDB) {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
var dbPromise = null;
function getDbPromise() {
if (!dbPromise) {
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, function (upgradeDB) {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
});
}
});
return dbPromise;
}
/** Assigns or overwrites the record for the given key with the given value. */

@@ -365,3 +383,3 @@ function set(appConfig, value) {

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -389,3 +407,3 @@ db = _a.sent();

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -418,3 +436,3 @@ db = _a.sent();

key = getKey(appConfig);
return [4 /*yield*/, dbPromise];
return [4 /*yield*/, getDbPromise()];
case 1:

@@ -474,16 +492,23 @@ db = _a.sent();

return __awaiter(this, void 0, void 0, function () {
var registrationPromise, _a;
var registrationPromise, installationEntry, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
case 0: return [4 /*yield*/, update(appConfig, function (oldEntry) {
var installationEntry = updateOrCreateInstallationEntry(oldEntry);
var entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
})];
case 1:
installationEntry = _b.sent();
if (!(installationEntry.fid === INVALID_FID)) return [3 /*break*/, 3];
_a = {};
return [4 /*yield*/, update(appConfig, function (oldEntry) {
var installationEntry = updateOrCreateFid(oldEntry);
var entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
})];
case 1: return [2 /*return*/, (_a.installationEntry = _b.sent(),
_a.registrationPromise = registrationPromise,
_a)];
return [4 /*yield*/, registrationPromise];
case 2:
// FID generation failed. Waiting for the FID from the server.
return [2 /*return*/, (_a.installationEntry = _b.sent(), _a)];
case 3: return [2 /*return*/, {
installationEntry: installationEntry,
registrationPromise: registrationPromise
}];
}

@@ -493,3 +518,3 @@ });

}
function updateOrCreateFid(oldEntry) {
function updateOrCreateInstallationEntry(oldEntry) {
var entry = oldEntry || {

@@ -547,22 +572,19 @@ fid: generateFid(),

case 0:
_a.trys.push([0, 3, , 8]);
_a.trys.push([0, 2, , 7]);
return [4 /*yield*/, createInstallation(appConfig, installationEntry)];
case 1:
registeredInstallationEntry = _a.sent();
return [4 /*yield*/, set(appConfig, registeredInstallationEntry)];
return [2 /*return*/, set(appConfig, registeredInstallationEntry)];
case 2:
_a.sent();
return [3 /*break*/, 8];
case 3:
e_1 = _a.sent();
if (!(isServerError(e_1) && e_1.serverCode === 409)) return [3 /*break*/, 5];
if (!(isServerError(e_1) && e_1.serverCode === 409)) return [3 /*break*/, 4];
// Server returned a "FID can not be used" error.
// Generate a new ID next time.
return [4 /*yield*/, remove(appConfig)];
case 4:
case 3:
// Server returned a "FID can not be used" error.
// Generate a new ID next time.
_a.sent();
return [3 /*break*/, 7];
case 5:
return [3 /*break*/, 6];
case 4:
// Registration failed. Set FID as not registered.

@@ -573,8 +595,8 @@ return [4 /*yield*/, set(appConfig, {

})];
case 6:
case 5:
// Registration failed. Set FID as not registered.
_a.sent();
_a.label = 7;
case 7: throw e_1;
case 8: return [2 /*return*/];
_a.label = 6;
case 6: throw e_1;
case 7: return [2 /*return*/];
}

@@ -609,3 +631,3 @@ });

}
return [2 /*return*/];
return [2 /*return*/, entry];
}

@@ -612,0 +634,0 @@ });

@@ -5,3 +5,3 @@ import firebase from '@firebase/app';

const version = "0.1.7-canary.ea3adf1";
const version = "0.1.7-canary.ed17a58";

@@ -197,3 +197,3 @@ /**

const registeredInstallationEntry = {
fid,
fid: responseValue.fid || fid,
registrationStatus: 2 /* COMPLETED */,

@@ -249,4 +249,3 @@ refreshToken: responseValue.refreshToken,

*/
function bufferToBase64UrlSafe(buffer) {
const array = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
function bufferToBase64UrlSafe(array) {
const b64 = btoa(String.fromCharCode(...array));

@@ -272,11 +271,24 @@ return b64.replace(/\+/g, '-').replace(/\//g, '_');

*/
/** Generates a new FID using random values from Web Crypto API. */
const VALID_FID_PATTERN = /^[cdef][\w-]{21}$/;
const INVALID_FID = '';
/**
* Generates a new FID using random values from Web Crypto API.
* Returns an empty string if FID generation fails for any reason.
*/
function generateFid() {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
const fidByteArray = new Uint8Array(17);
crypto.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 0b01110000 + (fidByteArray[0] % 0b00010000);
return encode(fidByteArray);
try {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
const fidByteArray = new Uint8Array(17);
const crypto = self.crypto || self.msCrypto;
crypto.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 0b01110000 + (fidByteArray[0] % 0b00010000);
const fid = encode(fidByteArray);
return VALID_FID_PATTERN.test(fid) ? fid : INVALID_FID;
}
catch (_a) {
// FID generation errored
return INVALID_FID;
}
}

@@ -310,17 +322,23 @@ /** Converts a FID Uint8Array to a base64 string representation. */

const OBJECT_STORE_NAME = 'firebase-installations-store';
const dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
let dbPromise = null;
function getDbPromise() {
if (!dbPromise) {
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
});
}
});
return dbPromise;
}
/** Assigns or overwrites the record for the given key with the given value. */
async function set(appConfig, value) {
const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -334,3 +352,3 @@ await tx.objectStore(OBJECT_STORE_NAME).put(value, key);

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -348,3 +366,3 @@ await tx.objectStore(OBJECT_STORE_NAME).delete(key);

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -392,13 +410,18 @@ const store = tx.objectStore(OBJECT_STORE_NAME);

let registrationPromise;
const installationEntry = await update(appConfig, (oldEntry) => {
const installationEntry = updateOrCreateInstallationEntry(oldEntry);
const entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
});
if (installationEntry.fid === INVALID_FID) {
// FID generation failed. Waiting for the FID from the server.
return { installationEntry: await registrationPromise };
}
return {
installationEntry: await update(appConfig, (oldEntry) => {
const installationEntry = updateOrCreateFid(oldEntry);
const entryWithPromise = triggerRegistrationIfNecessary(appConfig, installationEntry);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
}),
installationEntry,
registrationPromise
};
}
function updateOrCreateFid(oldEntry) {
function updateOrCreateInstallationEntry(oldEntry) {
const entry = oldEntry || {

@@ -453,3 +476,3 @@ fid: generateFid(),

const registeredInstallationEntry = await createInstallation(appConfig, installationEntry);
await set(appConfig, registeredInstallationEntry);
return set(appConfig, registeredInstallationEntry);
}

@@ -486,2 +509,3 @@ catch (e) {

}
return entry;
}

@@ -488,0 +512,0 @@ /**

@@ -17,2 +17,2 @@ /**

*/
export declare function bufferToBase64UrlSafe(buffer: ArrayBuffer | Uint8Array): string;
export declare function bufferToBase64UrlSafe(array: Uint8Array): string;

@@ -17,3 +17,8 @@ /**

*/
/** Generates a new FID using random values from Web Crypto API. */
export declare const VALID_FID_PATTERN: RegExp;
export declare const INVALID_FID = "";
/**
* Generates a new FID using random values from Web Crypto API.
* Returns an empty string if FID generation fails for any reason.
*/
export declare function generateFid(): string;

@@ -18,6 +18,6 @@ /**

import { AppConfig } from '../interfaces/app-config';
import { InstallationEntry } from '../interfaces/installation-entry';
import { InstallationEntry, RegisteredInstallationEntry } from '../interfaces/installation-entry';
export interface InstallationEntryWithRegistrationPromise {
installationEntry: InstallationEntry;
registrationPromise?: Promise<void>;
registrationPromise?: Promise<RegisteredInstallationEntry>;
}

@@ -24,0 +24,0 @@ /**

@@ -20,2 +20,3 @@ /**

readonly authToken: GenerateAuthTokenResponse;
readonly fid?: string;
}

@@ -22,0 +23,0 @@ export interface GenerateAuthTokenResponse {

{
"name": "@firebase/installations",
"version": "0.1.7-canary.ea3adf1",
"version": "0.1.7-canary.ed17a58",
"main": "dist/index.cjs.js",

@@ -50,8 +50,8 @@ "module": "dist/index.esm.js",

"peerDependencies": {
"@firebase/app": "0.4.7-canary.ea3adf1",
"@firebase/app-types": "0.4.0-canary.ea3adf1"
"@firebase/app": "0.4.9-canary.ed17a58",
"@firebase/app-types": "0.4.0-canary.ed17a58"
},
"dependencies": {
"@firebase/installations-types": "0.1.1-canary.ea3adf1",
"@firebase/util": "0.2.20-canary.ea3adf1",
"@firebase/installations-types": "0.1.1-canary.ed17a58",
"@firebase/util": "0.2.20-canary.ed17a58",
"idb": "3.0.2",

@@ -58,0 +58,0 @@ "tslib": "1.9.3"

@@ -61,3 +61,4 @@ /**

expiresIn: '604800s'
}
},
fid: FID
};

@@ -108,2 +109,20 @@ fetchSpy = stub(self, 'fetch');

it('returns the FID from the request if the response does not contain one', async () => {
response = {
refreshToken: 'refreshToken',
authToken: {
token:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
expiresIn: '604800s'
}
};
fetchSpy.resolves(new Response(JSON.stringify(response)));
const registeredInstallationEntry = await createInstallation(
appConfig,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.fid).to.equal(FID);
});
describe('failed request', () => {

@@ -110,0 +129,0 @@ it('throws a FirebaseError with the error information from the server', async () => {

@@ -58,3 +58,3 @@ /**

const registeredInstallationEntry: RegisteredInstallationEntry = {
fid,
fid: responseValue.fid || fid,
registrationStatus: RequestStatus.COMPLETED,

@@ -61,0 +61,0 @@ refreshToken: responseValue.refreshToken,

@@ -31,8 +31,2 @@ /**

});
it('returns a base64 representation of an ArrayBuffer', () => {
expect(bufferToBase64UrlSafe(TYPED_ARRAY_REPRESENTATION.buffer)).to.equal(
BASE_64_REPRESENTATION
);
});
});

@@ -18,8 +18,5 @@ /**

export function bufferToBase64UrlSafe(
buffer: ArrayBuffer | Uint8Array
): string {
const array = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
export function bufferToBase64UrlSafe(array: Uint8Array): string {
const b64 = btoa(String.fromCharCode(...array));
return b64.replace(/\+/g, '-').replace(/\//g, '_');
}

@@ -21,3 +21,3 @@ /**

import '../testing/setup';
import { generateFid } from './generate-fid';
import { generateFid, VALID_FID_PATTERN } from './generate-fid';

@@ -57,4 +57,2 @@ /** A few random values to generate a FID from. */

const VALID_FID = /^[cdef][A-Za-z0-9_-]{21}$/;
describe('generateFid', () => {

@@ -82,3 +80,6 @@ it('deterministically generates FIDs based on crypto.getRandomValues', () => {

const fid = generateFid();
expect(VALID_FID.test(fid)).to.equal(true, `${fid} is not a valid FID`);
expect(VALID_FID_PATTERN.test(fid)).to.equal(
true,
`${fid} is not a valid FID`
);
}

@@ -123,2 +124,9 @@ });

}).timeout(30000);
it('returns an empty string if FID generation fails', () => {
stub(crypto, 'getRandomValues').throws();
const fid = generateFid();
expect(fid).to.equal('');
});
});

@@ -20,13 +20,28 @@ /**

/** Generates a new FID using random values from Web Crypto API. */
export const VALID_FID_PATTERN = /^[cdef][\w-]{21}$/;
export const INVALID_FID = '';
/**
* Generates a new FID using random values from Web Crypto API.
* Returns an empty string if FID generation fails for any reason.
*/
export function generateFid(): string {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
const fidByteArray = new Uint8Array(17);
crypto.getRandomValues(fidByteArray);
try {
// A valid FID has exactly 22 base64 characters, which is 132 bits, or 16.5
// bytes. our implementation generates a 17 byte array instead.
const fidByteArray = new Uint8Array(17);
const crypto =
self.crypto || ((self as unknown) as { msCrypto: Crypto }).msCrypto;
crypto.getRandomValues(fidByteArray);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 0b01110000 + (fidByteArray[0] % 0b00010000);
// Replace the first 4 random bits with the constant FID header of 0b0111.
fidByteArray[0] = 0b01110000 + (fidByteArray[0] % 0b00010000);
return encode(fidByteArray);
const fid = encode(fidByteArray);
return VALID_FID_PATTERN.test(fid) ? fid : INVALID_FID;
} catch {
// FID generation errored
return INVALID_FID;
}
}

@@ -33,0 +48,0 @@

@@ -32,3 +32,3 @@ /**

import { sleep } from '../util/sleep';
import * as fidGenerator from './generate-fid';
import * as generateFidModule from './generate-fid';
import { getInstallationEntry } from './get-installation-entry';

@@ -57,3 +57,4 @@ import { get, set } from './idb-manager';

const registeredInstallationEntry: RegisteredInstallationEntry = {
fid: installationEntry.fid,
// Returns new FID if client FID is invalid.
fid: installationEntry.fid || FID,
registrationStatus: RequestStatus.COMPLETED,

@@ -184,5 +185,6 @@ refreshToken: 'refreshToken',

beforeEach(() => {
generateInstallationEntrySpy = stub(fidGenerator, 'generateFid').returns(
FID
);
generateInstallationEntrySpy = stub(
generateFidModule,
'generateFid'
).returns(FID);
});

@@ -261,2 +263,21 @@

});
it('waits for the FID from the server if FID generation fails', async () => {
clock.restore();
// Needed to allow the createInstallation request to complete.
clock = useFakeTimers({ shouldAdvanceTime: true });
// FID generation fails.
generateInstallationEntrySpy.returns(generateFidModule.INVALID_FID);
const getInstallationEntryPromise = getInstallationEntry(appConfig);
const {
installationEntry,
registrationPromise
} = await getInstallationEntryPromise;
expect(installationEntry.fid).to.equal(FID);
expect(registrationPromise).to.be.undefined;
});
});

@@ -263,0 +284,0 @@

@@ -23,3 +23,4 @@ /**

InstallationEntry,
RequestStatus
RequestStatus,
RegisteredInstallationEntry
} from '../interfaces/installation-entry';

@@ -29,3 +30,3 @@ import { PENDING_TIMEOUT_MS } from '../util/constants';

import { sleep } from '../util/sleep';
import { generateFid } from './generate-fid';
import { generateFid, INVALID_FID } from './generate-fid';
import { remove, set, update } from './idb-manager';

@@ -35,3 +36,3 @@

installationEntry: InstallationEntry;
registrationPromise?: Promise<void>;
registrationPromise?: Promise<RegisteredInstallationEntry>;
}

@@ -46,17 +47,24 @@

): Promise<InstallationEntryWithRegistrationPromise> {
let registrationPromise: Promise<void> | undefined;
let registrationPromise: Promise<RegisteredInstallationEntry> | undefined;
const installationEntry = await update(
appConfig,
(oldEntry?: InstallationEntry): InstallationEntry => {
const installationEntry = updateOrCreateInstallationEntry(oldEntry);
const entryWithPromise = triggerRegistrationIfNecessary(
appConfig,
installationEntry
);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
}
);
if (installationEntry.fid === INVALID_FID) {
// FID generation failed. Waiting for the FID from the server.
return { installationEntry: await registrationPromise! };
}
return {
installationEntry: await update(
appConfig,
(oldEntry?: InstallationEntry): InstallationEntry => {
const installationEntry = updateOrCreateFid(oldEntry);
const entryWithPromise = triggerRegistrationIfNecessary(
appConfig,
installationEntry
);
registrationPromise = entryWithPromise.registrationPromise;
return entryWithPromise.installationEntry;
}
),
installationEntry,
registrationPromise

@@ -66,3 +74,3 @@ };

function updateOrCreateFid(
function updateOrCreateInstallationEntry(
oldEntry: InstallationEntry | undefined

@@ -132,3 +140,3 @@ ): InstallationEntry {

installationEntry: InProgressInstallationEntry
): Promise<void> {
): Promise<RegisteredInstallationEntry> {
try {

@@ -139,3 +147,3 @@ const registeredInstallationEntry = await createInstallation(

);
await set(appConfig, registeredInstallationEntry);
return set(appConfig, registeredInstallationEntry);
} catch (e) {

@@ -158,3 +166,5 @@ if (isServerError(e) && e.serverCode === 409) {

/** Call if FID registration is pending. */
async function waitUntilFidRegistration(appConfig: AppConfig): Promise<void> {
async function waitUntilFidRegistration(
appConfig: AppConfig
): Promise<RegisteredInstallationEntry> {
// Unfortunately, there is no way of reliably observing when a value in

@@ -175,2 +185,4 @@ // IndexedDB changes (yet, see https://github.com/WICG/indexed-db-observers),

}
return entry;
}

@@ -177,0 +189,0 @@

@@ -25,17 +25,19 @@ /**

const dbPromise: Promise<DB> = openDb(
DATABASE_NAME,
DATABASE_VERSION,
upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
let dbPromise: Promise<DB> | null = null;
function getDbPromise(): Promise<DB> {
if (!dbPromise) {
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
// We don't use 'break' in this switch statement, the fall-through
// behavior is what we want, because if there are multiple versions between
// the old version and the current version, we want ALL the migrations
// that correspond to those versions to run, not only the last one.
// eslint-disable-next-line default-case
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
}
});
}
);
return dbPromise;
}

@@ -45,3 +47,3 @@ /** Gets record(s) from the objectStore that match the given key. */

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
return db

@@ -59,3 +61,3 @@ .transaction(OBJECT_STORE_NAME)

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -70,3 +72,3 @@ await tx.objectStore(OBJECT_STORE_NAME).put(value, key);

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -88,3 +90,3 @@ await tx.objectStore(OBJECT_STORE_NAME).delete(key);

const key = getKey(appConfig);
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -110,3 +112,3 @@ const store = tx.objectStore(OBJECT_STORE_NAME);

export async function clear(): Promise<void> {
const db = await dbPromise;
const db = await getDbPromise();
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');

@@ -113,0 +115,0 @@ await tx.objectStore(OBJECT_STORE_NAME).clear();

@@ -21,2 +21,3 @@ /**

readonly authToken: GenerateAuthTokenResponse;
readonly fid?: string;
}

@@ -23,0 +24,0 @@

{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"lib": ["es2017", "dom"],
"downlevelIteration": true,

@@ -6,0 +5,0 @@ "resolveJsonModule": true,

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc