UNPKG

15.6 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
2"use strict";
3/*!
4 * Copyright 2020 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.Model = exports.MachineLearning = void 0;
20var index_1 = require("../storage/index");
21var error_1 = require("../utils/error");
22var validator = require("../utils/validator");
23var deep_copy_1 = require("../utils/deep-copy");
24var utils = require("../utils");
25var machine_learning_api_client_1 = require("./machine-learning-api-client");
26var machine_learning_utils_1 = require("./machine-learning-utils");
27/**
28 * The Firebase `MachineLearning` service interface.
29 */
30var MachineLearning = /** @class */ (function () {
31 /**
32 * @param app - The app for this ML service.
33 * @constructor
34 * @internal
35 */
36 function MachineLearning(app) {
37 if (!validator.isNonNullObject(app) || !('options' in app)) {
38 throw new error_1.FirebaseError({
39 code: 'machine-learning/invalid-argument',
40 message: 'First argument passed to admin.machineLearning() must be a ' +
41 'valid Firebase app instance.',
42 });
43 }
44 this.appInternal = app;
45 this.client = new machine_learning_api_client_1.MachineLearningApiClient(app);
46 }
47 Object.defineProperty(MachineLearning.prototype, "app", {
48 /**
49 * The {@link firebase-admin.app#App} associated with the current `MachineLearning`
50 * service instance.
51 */
52 get: function () {
53 return this.appInternal;
54 },
55 enumerable: false,
56 configurable: true
57 });
58 /**
59 * Creates a model in the current Firebase project.
60 *
61 * @param model - The model to create.
62 *
63 * @returns A Promise fulfilled with the created model.
64 */
65 MachineLearning.prototype.createModel = function (model) {
66 var _this = this;
67 return this.signUrlIfPresent(model)
68 .then(function (modelContent) { return _this.client.createModel(modelContent); })
69 .then(function (operation) { return _this.client.handleOperation(operation); })
70 .then(function (modelResponse) { return new Model(modelResponse, _this.client); });
71 };
72 /**
73 * Updates a model's metadata or model file.
74 *
75 * @param modelId - The ID of the model to update.
76 * @param model - The model fields to update.
77 *
78 * @returns A Promise fulfilled with the updated model.
79 */
80 MachineLearning.prototype.updateModel = function (modelId, model) {
81 var _this = this;
82 var updateMask = utils.generateUpdateMask(model);
83 return this.signUrlIfPresent(model)
84 .then(function (modelContent) { return _this.client.updateModel(modelId, modelContent, updateMask); })
85 .then(function (operation) { return _this.client.handleOperation(operation); })
86 .then(function (modelResponse) { return new Model(modelResponse, _this.client); });
87 };
88 /**
89 * Publishes a Firebase ML model.
90 *
91 * A published model can be downloaded to client apps.
92 *
93 * @param modelId - The ID of the model to publish.
94 *
95 * @returns A Promise fulfilled with the published model.
96 */
97 MachineLearning.prototype.publishModel = function (modelId) {
98 return this.setPublishStatus(modelId, true);
99 };
100 /**
101 * Unpublishes a Firebase ML model.
102 *
103 * @param modelId - The ID of the model to unpublish.
104 *
105 * @returns A Promise fulfilled with the unpublished model.
106 */
107 MachineLearning.prototype.unpublishModel = function (modelId) {
108 return this.setPublishStatus(modelId, false);
109 };
110 /**
111 * Gets the model specified by the given ID.
112 *
113 * @param modelId - The ID of the model to get.
114 *
115 * @returns A Promise fulfilled with the model object.
116 */
117 MachineLearning.prototype.getModel = function (modelId) {
118 var _this = this;
119 return this.client.getModel(modelId)
120 .then(function (modelResponse) { return new Model(modelResponse, _this.client); });
121 };
122 /**
123 * Lists the current project's models.
124 *
125 * @param options - The listing options.
126 *
127 * @returns A promise that
128 * resolves with the current (filtered) list of models and the next page
129 * token. For the last page, an empty list of models and no page token
130 * are returned.
131 */
132 MachineLearning.prototype.listModels = function (options) {
133 var _this = this;
134 if (options === void 0) { options = {}; }
135 return this.client.listModels(options)
136 .then(function (resp) {
137 if (!validator.isNonNullObject(resp)) {
138 throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', "Invalid ListModels response: " + JSON.stringify(resp));
139 }
140 var models = [];
141 if (resp.models) {
142 models = resp.models.map(function (rs) { return new Model(rs, _this.client); });
143 }
144 var result = { models: models };
145 if (resp.nextPageToken) {
146 result.pageToken = resp.nextPageToken;
147 }
148 return result;
149 });
150 };
151 /**
152 * Deletes a model from the current project.
153 *
154 * @param modelId - The ID of the model to delete.
155 */
156 MachineLearning.prototype.deleteModel = function (modelId) {
157 return this.client.deleteModel(modelId);
158 };
159 MachineLearning.prototype.setPublishStatus = function (modelId, publish) {
160 var _this = this;
161 var updateMask = ['state.published'];
162 var options = { state: { published: publish } };
163 return this.client.updateModel(modelId, options, updateMask)
164 .then(function (operation) { return _this.client.handleOperation(operation); })
165 .then(function (modelResponse) { return new Model(modelResponse, _this.client); });
166 };
167 MachineLearning.prototype.signUrlIfPresent = function (options) {
168 var modelOptions = deep_copy_1.deepCopy(options);
169 if (machine_learning_api_client_1.isGcsTfliteModelOptions(modelOptions)) {
170 return this.signUrl(modelOptions.tfliteModel.gcsTfliteUri)
171 .then(function (uri) {
172 modelOptions.tfliteModel.gcsTfliteUri = uri;
173 return modelOptions;
174 })
175 .catch(function (err) {
176 throw new machine_learning_utils_1.FirebaseMachineLearningError('internal-error', "Error during signing upload url: " + err.message);
177 });
178 }
179 return Promise.resolve(modelOptions);
180 };
181 MachineLearning.prototype.signUrl = function (unsignedUrl) {
182 var MINUTES_IN_MILLIS = 60 * 1000;
183 var URL_VALID_DURATION = 10 * MINUTES_IN_MILLIS;
184 var gcsRegex = /^gs:\/\/([a-z0-9_.-]{3,63})\/(.+)$/;
185 var matches = gcsRegex.exec(unsignedUrl);
186 if (!matches) {
187 throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-argument', "Invalid unsigned url: " + unsignedUrl);
188 }
189 var bucketName = matches[1];
190 var blobName = matches[2];
191 var bucket = index_1.getStorage(this.app).bucket(bucketName);
192 var blob = bucket.file(blobName);
193 return blob.getSignedUrl({
194 action: 'read',
195 expires: Date.now() + URL_VALID_DURATION,
196 }).then(function (signUrl) { return signUrl[0]; });
197 };
198 return MachineLearning;
199}());
200exports.MachineLearning = MachineLearning;
201/**
202 * A Firebase ML Model output object.
203 */
204var Model = /** @class */ (function () {
205 /**
206 * @internal
207 */
208 function Model(model, client) {
209 this.model = Model.validateAndClone(model);
210 this.client = client;
211 }
212 Object.defineProperty(Model.prototype, "modelId", {
213 /** The ID of the model. */
214 get: function () {
215 return extractModelId(this.model.name);
216 },
217 enumerable: false,
218 configurable: true
219 });
220 Object.defineProperty(Model.prototype, "displayName", {
221 /**
222 * The model's name. This is the name you use from your app to load the
223 * model.
224 */
225 get: function () {
226 return this.model.displayName;
227 },
228 enumerable: false,
229 configurable: true
230 });
231 Object.defineProperty(Model.prototype, "tags", {
232 /**
233 * The model's tags, which can be used to group or filter models in list
234 * operations.
235 */
236 get: function () {
237 return this.model.tags || [];
238 },
239 enumerable: false,
240 configurable: true
241 });
242 Object.defineProperty(Model.prototype, "createTime", {
243 /** The timestamp of the model's creation. */
244 get: function () {
245 return new Date(this.model.createTime).toUTCString();
246 },
247 enumerable: false,
248 configurable: true
249 });
250 Object.defineProperty(Model.prototype, "updateTime", {
251 /** The timestamp of the model's most recent update. */
252 get: function () {
253 return new Date(this.model.updateTime).toUTCString();
254 },
255 enumerable: false,
256 configurable: true
257 });
258 Object.defineProperty(Model.prototype, "validationError", {
259 /** Error message when model validation fails. */
260 get: function () {
261 var _a, _b;
262 return (_b = (_a = this.model.state) === null || _a === void 0 ? void 0 : _a.validationError) === null || _b === void 0 ? void 0 : _b.message;
263 },
264 enumerable: false,
265 configurable: true
266 });
267 Object.defineProperty(Model.prototype, "published", {
268 /** True if the model is published. */
269 get: function () {
270 var _a;
271 return ((_a = this.model.state) === null || _a === void 0 ? void 0 : _a.published) || false;
272 },
273 enumerable: false,
274 configurable: true
275 });
276 Object.defineProperty(Model.prototype, "etag", {
277 /**
278 * The ETag identifier of the current version of the model. This value
279 * changes whenever you update any of the model's properties.
280 */
281 get: function () {
282 return this.model.etag;
283 },
284 enumerable: false,
285 configurable: true
286 });
287 Object.defineProperty(Model.prototype, "modelHash", {
288 /**
289 * The hash of the model's `tflite` file. This value changes only when
290 * you upload a new TensorFlow Lite model.
291 */
292 get: function () {
293 return this.model.modelHash;
294 },
295 enumerable: false,
296 configurable: true
297 });
298 Object.defineProperty(Model.prototype, "tfliteModel", {
299 /** Metadata about the model's TensorFlow Lite model file. */
300 get: function () {
301 // Make a copy so people can't directly modify the private this.model object.
302 return deep_copy_1.deepCopy(this.model.tfliteModel);
303 },
304 enumerable: false,
305 configurable: true
306 });
307 Object.defineProperty(Model.prototype, "locked", {
308 /**
309 * True if the model is locked by a server-side operation. You can't make
310 * changes to a locked model. See {@link Model.waitForUnlocked}.
311 */
312 get: function () {
313 var _a, _b;
314 return ((_b = (_a = this.model.activeOperations) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0;
315 },
316 enumerable: false,
317 configurable: true
318 });
319 /**
320 * Return the model as a JSON object.
321 */
322 Model.prototype.toJSON = function () {
323 // We can't just return this.model because it has extra fields and
324 // different formats etc. So we build the expected model object.
325 var jsonModel = {
326 modelId: this.modelId,
327 displayName: this.displayName,
328 tags: this.tags,
329 createTime: this.createTime,
330 updateTime: this.updateTime,
331 published: this.published,
332 etag: this.etag,
333 locked: this.locked,
334 };
335 // Also add possibly undefined fields if they exist.
336 if (this.validationError) {
337 jsonModel['validationError'] = this.validationError;
338 }
339 if (this.modelHash) {
340 jsonModel['modelHash'] = this.modelHash;
341 }
342 if (this.tfliteModel) {
343 jsonModel['tfliteModel'] = this.tfliteModel;
344 }
345 return jsonModel;
346 };
347 /**
348 * Wait for the model to be unlocked.
349 *
350 * @param maxTimeMillis - The maximum time in milliseconds to wait.
351 * If not specified, a default maximum of 2 minutes is used.
352 *
353 * @returns A promise that resolves when the model is unlocked
354 * or the maximum wait time has passed.
355 */
356 Model.prototype.waitForUnlocked = function (maxTimeMillis) {
357 var _this = this;
358 var _a, _b;
359 if (((_b = (_a = this.model.activeOperations) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) {
360 // The client will always be defined on Models that have activeOperations
361 // because models with active operations came back from the server and
362 // were constructed with a non-empty client.
363 return this.client.handleOperation(this.model.activeOperations[0], { wait: true, maxTimeMillis: maxTimeMillis })
364 .then(function (modelResponse) {
365 _this.model = Model.validateAndClone(modelResponse);
366 });
367 }
368 return Promise.resolve();
369 };
370 Model.validateAndClone = function (model) {
371 if (!validator.isNonNullObject(model) ||
372 !validator.isNonEmptyString(model.name) ||
373 !validator.isNonEmptyString(model.createTime) ||
374 !validator.isNonEmptyString(model.updateTime) ||
375 !validator.isNonEmptyString(model.displayName) ||
376 !validator.isNonEmptyString(model.etag)) {
377 throw new machine_learning_utils_1.FirebaseMachineLearningError('invalid-server-response', "Invalid Model response: " + JSON.stringify(model));
378 }
379 var tmpModel = deep_copy_1.deepCopy(model);
380 // If tflite Model is specified, it must have a source consisting of
381 // oneof {gcsTfliteUri, automlModel}
382 if (model.tfliteModel &&
383 !validator.isNonEmptyString(model.tfliteModel.gcsTfliteUri) &&
384 !validator.isNonEmptyString(model.tfliteModel.automlModel)) {
385 // If we have some other source, ignore the whole tfliteModel.
386 delete tmpModel.tfliteModel;
387 }
388 // Remove '@type' field. We don't need it.
389 if (tmpModel['@type']) {
390 delete tmpModel['@type'];
391 }
392 return tmpModel;
393 };
394 return Model;
395}());
396exports.Model = Model;
397function extractModelId(resourceName) {
398 return resourceName.split('/').pop();
399}