initial commit

This commit is contained in:
2025-09-01 22:12:29 +02:00
parent b1873f9c1d
commit 02a54f61c0
5598 changed files with 903558 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*! firebase-admin v13.5.0 */
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Firebase Machine Learning.
*
* @packageDocumentation
*/
import { App } from '../app';
import { MachineLearning } from './machine-learning';
export { MachineLearning, ListModelsResult, Model, TFLiteModel, } from './machine-learning';
export { GcsTfliteModelOptions, ListModelsOptions, ModelOptions, ModelOptionsBase, } from './machine-learning-api-client';
/**
* Gets the {@link MachineLearning} service for the default app or a given app.
*
* `getMachineLearning()` can be called with no arguments to access the
* default app's `MachineLearning` service or as `getMachineLearning(app)` to access
* the `MachineLearning` service associated with a specific app.
*
* @example
* ```javascript
* // Get the MachineLearning service for the default app
* const defaultMachineLearning = getMachineLearning();
* ```
*
* @example
* ```javascript
* // Get the MachineLearning service for a given app
* const otherMachineLearning = getMachineLearning(otherApp);
* ```
*
* @param app - Optional app whose `MachineLearning` service to
* return. If not provided, the default `MachineLearning` service
* will be returned.
*
* @returns The default `MachineLearning` service if no app is provided or the
* `MachineLearning` service associated with the provided app.
*/
export declare function getMachineLearning(app?: App): MachineLearning;

View File

@@ -0,0 +1,63 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = exports.MachineLearning = void 0;
exports.getMachineLearning = getMachineLearning;
/**
* Firebase Machine Learning.
*
* @packageDocumentation
*/
const app_1 = require("../app");
const machine_learning_1 = require("./machine-learning");
var machine_learning_2 = require("./machine-learning");
Object.defineProperty(exports, "MachineLearning", { enumerable: true, get: function () { return machine_learning_2.MachineLearning; } });
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return machine_learning_2.Model; } });
/**
* Gets the {@link MachineLearning} service for the default app or a given app.
*
* `getMachineLearning()` can be called with no arguments to access the
* default app's `MachineLearning` service or as `getMachineLearning(app)` to access
* the `MachineLearning` service associated with a specific app.
*
* @example
* ```javascript
* // Get the MachineLearning service for the default app
* const defaultMachineLearning = getMachineLearning();
* ```
*
* @example
* ```javascript
* // Get the MachineLearning service for a given app
* const otherMachineLearning = getMachineLearning(otherApp);
* ```
*
* @param app - Optional app whose `MachineLearning` service to
* return. If not provided, the default `MachineLearning` service
* will be returned.
*
* @returns The default `MachineLearning` service if no app is provided or the
* `MachineLearning` service associated with the provided app.
*/
function getMachineLearning(app) {
if (typeof app === 'undefined') {
app = (0, app_1.getApp)();
}
const firebaseApp = app;
return firebaseApp.getOrInitService('machineLearning', (app) => new machine_learning_1.MachineLearning(app));
}

View File

@@ -0,0 +1,96 @@
/*! firebase-admin v13.5.0 */
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Firebase ML Model input objects
*/
export interface ModelOptionsBase {
displayName?: string;
tags?: string[];
}
export interface GcsTfliteModelOptions extends ModelOptionsBase {
tfliteModel: {
gcsTfliteUri: string;
};
}
export type ModelOptions = ModelOptionsBase | GcsTfliteModelOptions;
/**
* Interface representing options for listing Models.
*/
export interface ListModelsOptions {
/**
* An expression that specifies how to filter the results.
*
* Examples:
*
* ```
* display_name = your_model
* display_name : experimental_*
* tags: face_detector AND tags: experimental
* state.published = true
* ```
*
* See https://firebase.google.com/docs/ml/manage-hosted-models#list_your_projects_models
*/
filter?: string;
/** The number of results to return in each page. */
pageSize?: number;
/** A token that specifies the result page to return. */
pageToken?: string;
}
export interface StatusErrorResponse {
readonly code: number;
readonly message: string;
}
export type ModelUpdateOptions = ModelOptions & {
state?: {
published?: boolean;
};
};
export declare function isGcsTfliteModelOptions(options: ModelOptions): options is GcsTfliteModelOptions;
export interface ModelContent {
readonly displayName?: string;
readonly tags?: string[];
readonly state?: {
readonly validationError?: StatusErrorResponse;
readonly published?: boolean;
};
readonly tfliteModel?: {
readonly gcsTfliteUri?: string;
readonly sizeBytes: number;
};
}
export interface ModelResponse extends ModelContent {
readonly name: string;
readonly createTime: string;
readonly updateTime: string;
readonly etag: string;
readonly modelHash?: string;
readonly activeOperations?: OperationResponse[];
}
export interface ListModelsResponse {
readonly models?: ModelResponse[];
readonly nextPageToken?: string;
}
export interface OperationResponse {
readonly name?: string;
readonly metadata?: {
[key: string]: any;
};
readonly done: boolean;
readonly error?: StatusErrorResponse;
readonly response?: ModelResponse;
}

View File

@@ -0,0 +1,290 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MachineLearningApiClient = void 0;
exports.isGcsTfliteModelOptions = isGcsTfliteModelOptions;
const api_request_1 = require("../utils/api-request");
const error_1 = require("../utils/error");
const utils = require("../utils/index");
const validator = require("../utils/validator");
const machine_learning_utils_1 = require("./machine-learning-utils");
const ML_V1BETA2_API = 'https://firebaseml.googleapis.com/v1beta2';
const FIREBASE_VERSION_HEADER = {
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`,
};
// Operation polling defaults
const POLL_DEFAULT_MAX_TIME_MILLISECONDS = 120000; // Maximum overall 2 minutes
const POLL_BASE_WAIT_TIME_MILLISECONDS = 3000; // Start with 3 second delay
const POLL_MAX_WAIT_TIME_MILLISECONDS = 30000; // Maximum 30 second delay
function isGcsTfliteModelOptions(options) {
const gcsUri = options?.tfliteModel?.gcsTfliteUri;
return typeof gcsUri !== 'undefined';
}
/**
* Class that facilitates sending requests to the Firebase ML backend API.
*
* @internal
*/
class MachineLearningApiClient {
constructor(app) {
this.app = app;
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'First argument passed to admin.machineLearning() must be a valid '
+ 'Firebase app instance.');
}
this.httpClient = new api_request_1.AuthorizedHttpClient(app);
}
createModel(model) {
if (!validator.isNonNullObject(model) ||
!validator.isNonEmptyString(model.displayName)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Invalid model content.');
return Promise.reject(err);
}
return this.getProjectUrl()
.then((url) => {
const request = {
method: 'POST',
url: `${url}/models`,
data: model,
};
return this.sendRequest(request);
});
}
updateModel(modelId, model, updateMask) {
if (!validator.isNonEmptyString(modelId) ||
!validator.isNonNullObject(model) ||
!validator.isNonEmptyArray(updateMask)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Invalid model or mask content.');
return Promise.reject(err);
}
return this.getProjectUrl()
.then((url) => {
const request = {
method: 'PATCH',
url: `${url}/models/${modelId}?updateMask=${updateMask.join()}`,
data: model,
};
return this.sendRequest(request);
});
}
getModel(modelId) {
return Promise.resolve()
.then(() => {
return this.getModelName(modelId);
})
.then((modelName) => {
return this.getResourceWithShortName(modelName);
});
}
getOperation(operationName) {
return Promise.resolve()
.then(() => {
return this.getResourceWithFullName(operationName);
});
}
listModels(options = {}) {
if (!validator.isNonNullObject(options)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Invalid ListModelsOptions');
return Promise.reject(err);
}
if (typeof options.filter !== 'undefined' && !validator.isNonEmptyString(options.filter)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Invalid list filter.');
return Promise.reject(err);
}
if (typeof options.pageSize !== 'undefined') {
if (!validator.isNumber(options.pageSize)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Invalid page size.');
return Promise.reject(err);
}
if (options.pageSize < 1 || options.pageSize > 100) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Page size must be between 1 and 100.');
return Promise.reject(err);
}
}
if (typeof options.pageToken !== 'undefined' && !validator.isNonEmptyString(options.pageToken)) {
const err = new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Next page token must be a non-empty string.');
return Promise.reject(err);
}
return this.getProjectUrl()
.then((url) => {
const request = {
method: 'GET',
url: `${url}/models`,
data: options,
};
return this.sendRequest(request);
});
}
deleteModel(modelId) {
return this.getProjectUrl()
.then((url) => {
const modelName = this.getModelName(modelId);
const request = {
method: 'DELETE',
url: `${url}/${modelName}`,
};
return this.sendRequest(request);
});
}
/**
* Handles a Long Running Operation coming back from the server.
*
* @param op - The operation to handle
* @param options - The options for polling
*/
handleOperation(op, options) {
if (op.done) {
if (op.response) {
return Promise.resolve(op.response);
}
else if (op.error) {
const err = machine_learning_utils_1.FirebaseMachineLearningError.fromOperationError(op.error.code, op.error.message);
return Promise.reject(err);
}
// Done operations must have either a response or an error.
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-server-response', 'Invalid operation response.');
}
// Operation is not done
if (options?.wait) {
return this.pollOperationWithExponentialBackoff(op.name, options);
}
const metadata = op.metadata || {};
const metadataType = metadata['@type'] || '';
if (!metadataType.includes('ModelOperationMetadata')) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-server-response', `Unknown Metadata type: ${JSON.stringify(metadata)}`);
}
return this.getModel(extractModelId(metadata.name));
}
// baseWaitMillis and maxWaitMillis should only ever be modified by unit tests to run faster.
pollOperationWithExponentialBackoff(opName, options) {
const maxTimeMilliseconds = options?.maxTimeMillis ?? POLL_DEFAULT_MAX_TIME_MILLISECONDS;
const baseWaitMillis = options?.baseWaitMillis ?? POLL_BASE_WAIT_TIME_MILLISECONDS;
const maxWaitMillis = options?.maxWaitMillis ?? POLL_MAX_WAIT_TIME_MILLISECONDS;
const poller = new api_request_1.ExponentialBackoffPoller(baseWaitMillis, maxWaitMillis, maxTimeMilliseconds);
return poller.poll(() => {
return this.getOperation(opName)
.then((responseData) => {
if (!responseData.done) {
return null;
}
if (responseData.error) {
const err = machine_learning_utils_1.FirebaseMachineLearningError.fromOperationError(responseData.error.code, responseData.error.message);
throw err;
}
return responseData.response;
});
});
}
/**
* Gets the specified resource from the ML API. Resource names must be the short names without project
* ID prefix (e.g. `models/123456789`).
*
* @param {string} name Short name of the resource to get. e.g. 'models/12345'
* @returns {Promise<T>} A promise that fulfills with the resource.
*/
getResourceWithShortName(name) {
return this.getProjectUrl()
.then((url) => {
const request = {
method: 'GET',
url: `${url}/${name}`,
};
return this.sendRequest(request);
});
}
/**
* Gets the specified resource from the ML API. Resource names must be the full names including project
* number prefix.
* @param fullName - Full resource name of the resource to get. e.g. projects/123465/operations/987654
* @returns {Promise<T>} A promise that fulfulls with the resource.
*/
getResourceWithFullName(fullName) {
const request = {
method: 'GET',
url: `${ML_V1BETA2_API}/${fullName}`
};
return this.sendRequest(request);
}
sendRequest(request) {
request.headers = FIREBASE_VERSION_HEADER;
return this.httpClient.send(request)
.then((resp) => {
return resp.data;
})
.catch((err) => {
throw this.toFirebaseError(err);
});
}
toFirebaseError(err) {
if (err instanceof error_1.PrefixedFirebaseError) {
return err;
}
const response = err.response;
if (!response.isJson()) {
return new machine_learning_utils_1.FirebaseMachineLearningError('unknown-error', `Unexpected response with status: ${response.status} and body: ${response.text}`);
}
const error = response.data.error || {};
let code = 'unknown-error';
if (error.status && error.status in ERROR_CODE_MAPPING) {
code = ERROR_CODE_MAPPING[error.status];
}
const message = error.message || `Unknown server error: ${response.text}`;
return new machine_learning_utils_1.FirebaseMachineLearningError(code, message);
}
getProjectUrl() {
return this.getProjectIdPrefix()
.then((projectIdPrefix) => {
return `${ML_V1BETA2_API}/${projectIdPrefix}`;
});
}
getProjectIdPrefix() {
if (this.projectIdPrefix) {
return Promise.resolve(this.projectIdPrefix);
}
return utils.findProjectId(this.app)
.then((projectId) => {
if (!validator.isNonEmptyString(projectId)) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Failed to determine project ID. Initialize the SDK with service account credentials, or '
+ 'set project ID as an app option. Alternatively, set the GOOGLE_CLOUD_PROJECT '
+ 'environment variable.');
}
this.projectIdPrefix = `projects/${projectId}`;
return this.projectIdPrefix;
});
}
getModelName(modelId) {
if (!validator.isNonEmptyString(modelId)) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Model ID must be a non-empty string.');
}
if (modelId.indexOf('/') !== -1) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', 'Model ID must not contain any "/" characters.');
}
return `models/${modelId}`;
}
}
exports.MachineLearningApiClient = MachineLearningApiClient;
const ERROR_CODE_MAPPING = {
INVALID_ARGUMENT: 'invalid-argument',
NOT_FOUND: 'not-found',
RESOURCE_EXHAUSTED: 'resource-exhausted',
UNAUTHENTICATED: 'authentication-error',
UNKNOWN: 'unknown-error',
};
function extractModelId(resourceName) {
return resourceName.split('/').pop();
}

View File

@@ -0,0 +1,81 @@
/*! firebase-admin v13.5.0 */
/*!
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { ListModelsResult as TListModelsResult, MachineLearning as TMachineLearning, Model as TModel, TFLiteModel as TTFLiteModel } from './machine-learning';
import { GcsTfliteModelOptions as TGcsTfliteModelOptions, ListModelsOptions as TListModelsOptions, ModelOptions as TModelOptions, ModelOptionsBase as TModelOptionsBase } from './machine-learning-api-client';
/**
* Gets the {@link firebase-admin.machine-learning#MachineLearning} service for the
* default app or a given app.
*
* `admin.machineLearning()` can be called with no arguments to access the
* default app's `MachineLearning` service or as `admin.machineLearning(app)` to access
* the `MachineLearning` service associated with a specific app.
*
* @example
* ```javascript
* // Get the MachineLearning service for the default app
* var defaultMachineLearning = admin.machineLearning();
* ```
*
* @example
* ```javascript
* // Get the MachineLearning service for a given app
* var otherMachineLearning = admin.machineLearning(otherApp);
* ```
*
* @param app - Optional app whose `MachineLearning` service to
* return. If not provided, the default `MachineLearning` service
* will be returned.
*
* @returns The default `MachineLearning` service if no app is provided or the
* `MachineLearning` service associated with the provided app.
*/
export declare function machineLearning(app?: App): machineLearning.MachineLearning;
export declare namespace machineLearning {
/**
* Type alias to {@link firebase-admin.machine-learning#ListModelsResult}.
*/
type ListModelsResult = TListModelsResult;
/**
* Type alias to {@link firebase-admin.machine-learning#MachineLearning}.
*/
type MachineLearning = TMachineLearning;
/**
* Type alias to {@link firebase-admin.machine-learning#Model}.
*/
type Model = TModel;
/**
* Type alias to {@link firebase-admin.machine-learning#TFLiteModel}.
*/
type TFLiteModel = TTFLiteModel;
/**
* Type alias to {@link firebase-admin.machine-learning#GcsTfliteModelOptions}.
*/
type GcsTfliteModelOptions = TGcsTfliteModelOptions;
/**
* Type alias to {@link firebase-admin.machine-learning#ListModelsOptions}.
*/
type ListModelsOptions = TListModelsOptions;
/**
* Type alias to {@link firebase-admin.machine-learning#ModelOptions}.
*/
type ModelOptions = TModelOptions;
/**
* Type alias to {@link firebase-admin.machine-learning#ModelOptionsBase}.
*/
type ModelOptionsBase = TModelOptionsBase;
}

View File

@@ -0,0 +1,18 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -0,0 +1,22 @@
/*! firebase-admin v13.5.0 */
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { PrefixedFirebaseError } from '../utils/error';
export type MachineLearningErrorCode = 'already-exists' | 'authentication-error' | 'internal-error' | 'invalid-argument' | 'invalid-server-response' | 'not-found' | 'resource-exhausted' | 'service-unavailable' | 'unknown-error' | 'cancelled' | 'deadline-exceeded' | 'permission-denied' | 'failed-precondition' | 'aborted' | 'out-of-range' | 'data-loss' | 'unauthenticated';
export declare class FirebaseMachineLearningError extends PrefixedFirebaseError {
static fromOperationError(code: number, message: string): FirebaseMachineLearningError;
constructor(code: MachineLearningErrorCode, message: string);
}

View File

@@ -0,0 +1,47 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirebaseMachineLearningError = void 0;
const error_1 = require("../utils/error");
class FirebaseMachineLearningError extends error_1.PrefixedFirebaseError {
static fromOperationError(code, message) {
switch (code) {
case 1: return new FirebaseMachineLearningError('cancelled', message);
case 2: return new FirebaseMachineLearningError('unknown-error', message);
case 3: return new FirebaseMachineLearningError('invalid-argument', message);
case 4: return new FirebaseMachineLearningError('deadline-exceeded', message);
case 5: return new FirebaseMachineLearningError('not-found', message);
case 6: return new FirebaseMachineLearningError('already-exists', message);
case 7: return new FirebaseMachineLearningError('permission-denied', message);
case 8: return new FirebaseMachineLearningError('resource-exhausted', message);
case 9: return new FirebaseMachineLearningError('failed-precondition', message);
case 10: return new FirebaseMachineLearningError('aborted', message);
case 11: return new FirebaseMachineLearningError('out-of-range', message);
case 13: return new FirebaseMachineLearningError('internal-error', message);
case 14: return new FirebaseMachineLearningError('service-unavailable', message);
case 15: return new FirebaseMachineLearningError('data-loss', message);
case 16: return new FirebaseMachineLearningError('unauthenticated', message);
default:
return new FirebaseMachineLearningError('unknown-error', message);
}
}
constructor(code, message) {
super('machine-learning', code, message);
}
}
exports.FirebaseMachineLearningError = FirebaseMachineLearningError;

View File

@@ -0,0 +1,173 @@
/*! firebase-admin v13.5.0 */
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { ListModelsOptions, ModelOptions } from './machine-learning-api-client';
/** Response object for a listModels operation. */
export interface ListModelsResult {
/** A list of models in your project. */
readonly models: Model[];
/**
* A token you can use to retrieve the next page of results. If null, the
* current page is the final page.
*/
readonly pageToken?: string;
}
/**
* A TensorFlow Lite Model output object
*/
export interface TFLiteModel {
/** The size of the model. */
readonly sizeBytes: number;
/** The URI from which the model was originally provided to Firebase. */
readonly gcsTfliteUri?: string;
}
/**
* The Firebase `MachineLearning` service interface.
*/
export declare class MachineLearning {
private readonly client;
private readonly appInternal;
/**
* The {@link firebase-admin.app#App} associated with the current `MachineLearning`
* service instance.
*/
get app(): App;
/**
* Creates a model in the current Firebase project.
*
* @param model - The model to create.
*
* @returns A Promise fulfilled with the created model.
*/
createModel(model: ModelOptions): Promise<Model>;
/**
* Updates a model's metadata or model file.
*
* @param modelId - The ID of the model to update.
* @param model - The model fields to update.
*
* @returns A Promise fulfilled with the updated model.
*/
updateModel(modelId: string, model: ModelOptions): Promise<Model>;
/**
* Publishes a Firebase ML model.
*
* A published model can be downloaded to client apps.
*
* @param modelId - The ID of the model to publish.
*
* @returns A Promise fulfilled with the published model.
*/
publishModel(modelId: string): Promise<Model>;
/**
* Unpublishes a Firebase ML model.
*
* @param modelId - The ID of the model to unpublish.
*
* @returns A Promise fulfilled with the unpublished model.
*/
unpublishModel(modelId: string): Promise<Model>;
/**
* Gets the model specified by the given ID.
*
* @param modelId - The ID of the model to get.
*
* @returns A Promise fulfilled with the model object.
*/
getModel(modelId: string): Promise<Model>;
/**
* Lists the current project's models.
*
* @param options - The listing options.
*
* @returns A promise that
* resolves with the current (filtered) list of models and the next page
* token. For the last page, an empty list of models and no page token
* are returned.
*/
listModels(options?: ListModelsOptions): Promise<ListModelsResult>;
/**
* Deletes a model from the current project.
*
* @param modelId - The ID of the model to delete.
*/
deleteModel(modelId: string): Promise<void>;
private setPublishStatus;
private signUrlIfPresent;
private signUrl;
}
/**
* A Firebase ML Model output object.
*/
export declare class Model {
private model;
private readonly client?;
/** The ID of the model. */
get modelId(): string;
/**
* The model's name. This is the name you use from your app to load the
* model.
*/
get displayName(): string;
/**
* The model's tags, which can be used to group or filter models in list
* operations.
*/
get tags(): string[];
/** The timestamp of the model's creation. */
get createTime(): string;
/** The timestamp of the model's most recent update. */
get updateTime(): string;
/** Error message when model validation fails. */
get validationError(): string | undefined;
/** True if the model is published. */
get published(): boolean;
/**
* The ETag identifier of the current version of the model. This value
* changes whenever you update any of the model's properties.
*/
get etag(): string;
/**
* The hash of the model's `tflite` file. This value changes only when
* you upload a new TensorFlow Lite model.
*/
get modelHash(): string | undefined;
/** Metadata about the model's TensorFlow Lite model file. */
get tfliteModel(): TFLiteModel | undefined;
/**
* True if the model is locked by a server-side operation. You can't make
* changes to a locked model. See {@link Model.waitForUnlocked}.
*/
get locked(): boolean;
/**
* Return the model as a JSON object.
*/
toJSON(): {
[key: string]: any;
};
/**
* Wait for the model to be unlocked.
*
* @param maxTimeMillis - The maximum time in milliseconds to wait.
* If not specified, a default maximum of 2 minutes is used.
*
* @returns A promise that resolves when the model is unlocked
* or the maximum wait time has passed.
*/
waitForUnlocked(maxTimeMillis?: number): Promise<void>;
private static validateAndClone;
}

View File

@@ -0,0 +1,336 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Model = exports.MachineLearning = void 0;
const index_1 = require("../storage/index");
const error_1 = require("../utils/error");
const validator = require("../utils/validator");
const deep_copy_1 = require("../utils/deep-copy");
const utils = require("../utils");
const machine_learning_api_client_1 = require("./machine-learning-api-client");
const machine_learning_utils_1 = require("./machine-learning-utils");
/**
* The Firebase `MachineLearning` service interface.
*/
class MachineLearning {
/**
* @param app - The app for this ML service.
* @constructor
* @internal
*/
constructor(app) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new error_1.FirebaseError({
code: 'machine-learning/invalid-argument',
message: 'First argument passed to admin.machineLearning() must be a ' +
'valid Firebase app instance.',
});
}
this.appInternal = app;
this.client = new machine_learning_api_client_1.MachineLearningApiClient(app);
}
/**
* The {@link firebase-admin.app#App} associated with the current `MachineLearning`
* service instance.
*/
get app() {
return this.appInternal;
}
/**
* Creates a model in the current Firebase project.
*
* @param model - The model to create.
*
* @returns A Promise fulfilled with the created model.
*/
createModel(model) {
return this.signUrlIfPresent(model)
.then((modelContent) => this.client.createModel(modelContent))
.then((operation) => this.client.handleOperation(operation))
.then((modelResponse) => new Model(modelResponse, this.client));
}
/**
* Updates a model's metadata or model file.
*
* @param modelId - The ID of the model to update.
* @param model - The model fields to update.
*
* @returns A Promise fulfilled with the updated model.
*/
updateModel(modelId, model) {
const updateMask = utils.generateUpdateMask(model);
return this.signUrlIfPresent(model)
.then((modelContent) => this.client.updateModel(modelId, modelContent, updateMask))
.then((operation) => this.client.handleOperation(operation))
.then((modelResponse) => new Model(modelResponse, this.client));
}
/**
* Publishes a Firebase ML model.
*
* A published model can be downloaded to client apps.
*
* @param modelId - The ID of the model to publish.
*
* @returns A Promise fulfilled with the published model.
*/
publishModel(modelId) {
return this.setPublishStatus(modelId, true);
}
/**
* Unpublishes a Firebase ML model.
*
* @param modelId - The ID of the model to unpublish.
*
* @returns A Promise fulfilled with the unpublished model.
*/
unpublishModel(modelId) {
return this.setPublishStatus(modelId, false);
}
/**
* Gets the model specified by the given ID.
*
* @param modelId - The ID of the model to get.
*
* @returns A Promise fulfilled with the model object.
*/
getModel(modelId) {
return this.client.getModel(modelId)
.then((modelResponse) => new Model(modelResponse, this.client));
}
/**
* Lists the current project's models.
*
* @param options - The listing options.
*
* @returns A promise that
* resolves with the current (filtered) list of models and the next page
* token. For the last page, an empty list of models and no page token
* are returned.
*/
listModels(options = {}) {
return this.client.listModels(options)
.then((resp) => {
if (!validator.isNonNullObject(resp)) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', `Invalid ListModels response: ${JSON.stringify(resp)}`);
}
let models = [];
if (resp.models) {
models = resp.models.map((rs) => new Model(rs, this.client));
}
const result = { models };
if (resp.nextPageToken) {
result.pageToken = resp.nextPageToken;
}
return result;
});
}
/**
* Deletes a model from the current project.
*
* @param modelId - The ID of the model to delete.
*/
deleteModel(modelId) {
return this.client.deleteModel(modelId);
}
setPublishStatus(modelId, publish) {
const updateMask = ['state.published'];
const options = { state: { published: publish } };
return this.client.updateModel(modelId, options, updateMask)
.then((operation) => this.client.handleOperation(operation))
.then((modelResponse) => new Model(modelResponse, this.client));
}
signUrlIfPresent(options) {
const modelOptions = (0, deep_copy_1.deepCopy)(options);
if ((0, machine_learning_api_client_1.isGcsTfliteModelOptions)(modelOptions)) {
return this.signUrl(modelOptions.tfliteModel.gcsTfliteUri)
.then((uri) => {
modelOptions.tfliteModel.gcsTfliteUri = uri;
return modelOptions;
})
.catch((err) => {
throw new machine_learning_utils_1.FirebaseMachineLearningError('internal-error', `Error during signing upload url: ${err.message}`);
});
}
return Promise.resolve(modelOptions);
}
signUrl(unsignedUrl) {
const MINUTES_IN_MILLIS = 60 * 1000;
const URL_VALID_DURATION = 10 * MINUTES_IN_MILLIS;
const gcsRegex = /^gs:\/\/([a-z0-9_.-]{3,63})\/(.+)$/;
const matches = gcsRegex.exec(unsignedUrl);
if (!matches) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', `Invalid unsigned url: ${unsignedUrl}`);
}
const bucketName = matches[1];
const blobName = matches[2];
const bucket = (0, index_1.getStorage)(this.app).bucket(bucketName);
const blob = bucket.file(blobName);
return blob.getSignedUrl({
action: 'read',
expires: Date.now() + URL_VALID_DURATION,
}).then((signUrl) => signUrl[0]);
}
}
exports.MachineLearning = MachineLearning;
/**
* A Firebase ML Model output object.
*/
class Model {
/**
* @internal
*/
constructor(model, client) {
this.model = Model.validateAndClone(model);
this.client = client;
}
/** The ID of the model. */
get modelId() {
return extractModelId(this.model.name);
}
/**
* The model's name. This is the name you use from your app to load the
* model.
*/
get displayName() {
return this.model.displayName;
}
/**
* The model's tags, which can be used to group or filter models in list
* operations.
*/
get tags() {
return this.model.tags || [];
}
/** The timestamp of the model's creation. */
get createTime() {
return new Date(this.model.createTime).toUTCString();
}
/** The timestamp of the model's most recent update. */
get updateTime() {
return new Date(this.model.updateTime).toUTCString();
}
/** Error message when model validation fails. */
get validationError() {
return this.model.state?.validationError?.message;
}
/** True if the model is published. */
get published() {
return this.model.state?.published || false;
}
/**
* The ETag identifier of the current version of the model. This value
* changes whenever you update any of the model's properties.
*/
get etag() {
return this.model.etag;
}
/**
* The hash of the model's `tflite` file. This value changes only when
* you upload a new TensorFlow Lite model.
*/
get modelHash() {
return this.model.modelHash;
}
/** Metadata about the model's TensorFlow Lite model file. */
get tfliteModel() {
// Make a copy so people can't directly modify the private this.model object.
return (0, deep_copy_1.deepCopy)(this.model.tfliteModel);
}
/**
* True if the model is locked by a server-side operation. You can't make
* changes to a locked model. See {@link Model.waitForUnlocked}.
*/
get locked() {
return (this.model.activeOperations?.length ?? 0) > 0;
}
/**
* Return the model as a JSON object.
*/
toJSON() {
// We can't just return this.model because it has extra fields and
// different formats etc. So we build the expected model object.
const jsonModel = {
modelId: this.modelId,
displayName: this.displayName,
tags: this.tags,
createTime: this.createTime,
updateTime: this.updateTime,
published: this.published,
etag: this.etag,
locked: this.locked,
};
// Also add possibly undefined fields if they exist.
if (this.validationError) {
jsonModel['validationError'] = this.validationError;
}
if (this.modelHash) {
jsonModel['modelHash'] = this.modelHash;
}
if (this.tfliteModel) {
jsonModel['tfliteModel'] = this.tfliteModel;
}
return jsonModel;
}
/**
* Wait for the model to be unlocked.
*
* @param maxTimeMillis - The maximum time in milliseconds to wait.
* If not specified, a default maximum of 2 minutes is used.
*
* @returns A promise that resolves when the model is unlocked
* or the maximum wait time has passed.
*/
waitForUnlocked(maxTimeMillis) {
if ((this.model.activeOperations?.length ?? 0) > 0) {
// The client will always be defined on Models that have activeOperations
// because models with active operations came back from the server and
// were constructed with a non-empty client.
return this.client.handleOperation(this.model.activeOperations[0], { wait: true, maxTimeMillis })
.then((modelResponse) => {
this.model = Model.validateAndClone(modelResponse);
});
}
return Promise.resolve();
}
static validateAndClone(model) {
if (!validator.isNonNullObject(model) ||
!validator.isNonEmptyString(model.name) ||
!validator.isNonEmptyString(model.createTime) ||
!validator.isNonEmptyString(model.updateTime) ||
!validator.isNonEmptyString(model.displayName) ||
!validator.isNonEmptyString(model.etag)) {
throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-server-response', `Invalid Model response: ${JSON.stringify(model)}`);
}
const tmpModel = (0, deep_copy_1.deepCopy)(model);
// If tflite Model is specified, it must have a source of {gcsTfliteUri}
if (model.tfliteModel &&
!validator.isNonEmptyString(model.tfliteModel.gcsTfliteUri)) {
// If we have some other source, ignore the whole tfliteModel.
delete tmpModel.tfliteModel;
}
// Remove '@type' field. We don't need it.
if (tmpModel['@type']) {
delete tmpModel['@type'];
}
return tmpModel;
}
}
exports.Model = Model;
function extractModelId(resourceName) {
return resourceName.split('/').pop();
}