Skip to content

Commit 985a2fc

Browse files
author
Ace Nassri
authored
Make OCR sample support both Node 6 and Node 8 (#821)
1 parent bbed4eb commit 985a2fc

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

functions/ocr/app/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ function renameImageForSave (filename, lang) {
108108
* a file is uploaded to the Cloud Storage bucket you created
109109
* for uploading images.
110110
*
111-
* @param {object} event The Cloud Functions event.
112-
* @param {object} event.data A Google Cloud Storage File object.
111+
* @param {object} event.data (Node 6) A Google Cloud Storage File object.
112+
* @param {object} event (Node 8+) A Google Cloud Storage File object.
113113
*/
114114
exports.processImage = (event) => {
115-
let file = event.data;
115+
let file = event.data || event;
116116

117117
return Promise.resolve()
118118
.then(() => {
@@ -143,14 +143,14 @@ exports.processImage = (event) => {
143143
* by the TRANSLATE_TOPIC value in the config.json file. The
144144
* function translates text using the Google Translate API.
145145
*
146-
* @param {object} event The Cloud Functions event.
147-
* @param {object} event.data The Cloud Pub/Sub Message object.
148-
* @param {string} event.data.data The "data" property of the Cloud Pub/Sub
146+
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
147+
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
148+
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
149149
* Message. This property will be a base64-encoded string that you must decode.
150150
*/
151151
exports.translateText = (event) => {
152-
const pubsubMessage = event.data;
153-
const jsonStr = Buffer.from(pubsubMessage.data, 'base64').toString();
152+
const pubsubData = event.data.data || event.data;
153+
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
154154
const payload = JSON.parse(jsonStr);
155155

156156
return Promise.resolve()
@@ -195,14 +195,14 @@ exports.translateText = (event) => {
195195
* by the RESULT_TOPIC value in the config.json file. The
196196
* function saves the data packet to a file in GCS.
197197
*
198-
* @param {object} event The Cloud Functions event.
199-
* @param {object} event.data The Cloud Pub/Sub Message object.
200-
* @param {string} event.data.data The "data" property of the Cloud Pub/Sub
198+
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
199+
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
200+
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
201201
* Message. This property will be a base64-encoded string that you must decode.
202202
*/
203203
exports.saveResult = (event) => {
204-
const pubsubMessage = event.data;
205-
const jsonStr = Buffer.from(pubsubMessage.data, 'base64').toString();
204+
const pubsubData = event.data.data || event.data;
205+
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
206206
const payload = JSON.parse(jsonStr);
207207

208208
return Promise.resolve()

functions/ocr/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"node": ">=4.3.2"
1313
},
1414
"scripts": {
15-
"lint": "samples lint",
15+
"lint": "repo-tools lint",
1616
"pretest": "npm run lint",
1717
"test": "ava -T 20s --verbose test/*.test.js"
1818
},

functions/ocr/app/test/index.test.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ test.serial(`processImage fails without a name`, async (t) => {
106106
t.deepEqual(err, error);
107107
});
108108

109-
test.serial(`processImage processes an image`, async (t) => {
109+
test.serial(`processImage processes an image with Node 6 arguments`, async (t) => {
110110
const event = {
111111
data: {
112112
bucket: bucketName,
@@ -123,6 +123,21 @@ test.serial(`processImage processes an image`, async (t) => {
123123
t.deepEqual(console.log.getCall(3).args, [`File ${event.data.name} processed.`]);
124124
});
125125

126+
test.serial(`processImage processes an image with Node 8 arguments`, async (t) => {
127+
const data = {
128+
bucket: bucketName,
129+
name: filename
130+
};
131+
const sample = getSample();
132+
133+
await sample.program.processImage(data);
134+
t.is(console.log.callCount, 4);
135+
t.deepEqual(console.log.getCall(0).args, [`Looking for text in image ${filename}`]);
136+
t.deepEqual(console.log.getCall(1).args, [`Extracted text from image (${text.length} chars)`]);
137+
t.deepEqual(console.log.getCall(2).args, [`Detected language "ja" for ${filename}`]);
138+
t.deepEqual(console.log.getCall(3).args, [`File ${data.name} processed.`]);
139+
});
140+
126141
test.serial(`translateText fails without text`, async (t) => {
127142
const error = new Error(`Text not provided. Make sure you have a "text" property in your request`);
128143
const event = {
@@ -157,7 +172,7 @@ test.serial(`translateText fails without a lang`, async (t) => {
157172
t.deepEqual(err, error);
158173
});
159174

160-
test.serial(`translateText translates and publishes text`, async (t) => {
175+
test.serial(`translateText translates and publishes text with Node 6 arguments`, async (t) => {
161176
const event = {
162177
data: {
163178
data: Buffer.from(
@@ -179,6 +194,26 @@ test.serial(`translateText translates and publishes text`, async (t) => {
179194
t.deepEqual(console.log.secondCall.args, [`Text translated to ${lang}`]);
180195
});
181196

197+
test.serial(`translateText translates and publishes text with Node 8 arguments`, async (t) => {
198+
const data = {
199+
data: Buffer.from(
200+
JSON.stringify({
201+
text,
202+
filename,
203+
lang
204+
})
205+
).toString(`base64`)
206+
};
207+
const sample = getSample();
208+
209+
sample.mocks.translate.translate.returns(Promise.resolve([translation]));
210+
211+
await sample.program.translateText(data);
212+
t.is(console.log.callCount, 2);
213+
t.deepEqual(console.log.firstCall.args, [`Translating text into ${lang}`]);
214+
t.deepEqual(console.log.secondCall.args, [`Text translated to ${lang}`]);
215+
});
216+
182217
test.serial(`saveResult fails without text`, async (t) => {
183218
const error = new Error(`Text not provided. Make sure you have a "text" property in your request`);
184219
const event = {
@@ -215,7 +250,7 @@ test.serial(`saveResult fails without a lang`, async (t) => {
215250
t.deepEqual(err, error);
216251
});
217252

218-
test.serial(`saveResult translates and publishes text`, async (t) => {
253+
test.serial(`saveResult translates and publishes text with Node 6 arguments`, async (t) => {
219254
const event = {
220255
data: {
221256
data: Buffer.from(JSON.stringify({ text, filename, lang })).toString(`base64`)
@@ -230,6 +265,19 @@ test.serial(`saveResult translates and publishes text`, async (t) => {
230265
t.deepEqual(console.log.getCall(2).args, [`File saved.`]);
231266
});
232267

268+
test.serial(`saveResult translates and publishes text with Node 8 arguments`, async (t) => {
269+
const data = {
270+
data: Buffer.from(JSON.stringify({ text, filename, lang })).toString(`base64`)
271+
};
272+
const sample = getSample();
273+
274+
await sample.program.saveResult(data);
275+
t.is(console.log.callCount, 3);
276+
t.deepEqual(console.log.getCall(0).args, [`Received request to save file ${filename}`]);
277+
t.deepEqual(console.log.getCall(1).args, [`Saving result to ${filename}_to_${lang}.txt in bucket ${sample.mocks.config.RESULT_BUCKET}`]);
278+
t.deepEqual(console.log.getCall(2).args, [`File saved.`]);
279+
});
280+
233281
test.serial(`saveResult translates and publishes text with dot in filename`, async (t) => {
234282
const event = {
235283
data: {

0 commit comments

Comments
 (0)