From 52039779758666ac35927f8ccf1f89a351682983 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Wed, 12 Jul 2023 12:04:58 +1000 Subject: [PATCH 1/5] get query params from websocket requests --- package.json | 2 +- src/faas/v0/context.ts | 23 ++++++++++++++++++-- src/gen/proto/faas/v1/faas_pb.d.ts | 3 +++ src/gen/proto/faas/v1/faas_pb.js | 35 +++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index be5de870..fe83422e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@nitric/sdk", "description": "Nitric NodeJS client sdk", - "nitric": "v0.30.0", + "nitric": "v0.32.0-rc.3", "author": "Nitric ", "repository": "https://github.com/nitrictech/node-sdk", "main": "lib/index.js", diff --git a/src/faas/v0/context.ts b/src/faas/v0/context.ts index 7ef161ff..c8e4e7e2 100644 --- a/src/faas/v0/context.ts +++ b/src/faas/v0/context.ts @@ -625,12 +625,28 @@ export class WebsocketNotificationContext extends TriggerContext< ): WebsocketNotificationContext { const ctx = new WebsocketNotificationContext(); + const query = ( + trigger + .getWebsocket() + .getQueryParamsMap() + // getEntryList claims to return [string, faas.HeaderValue][], but really returns [string, string[][]][] + // we force the type to match the real return type. + .getEntryList() as unknown as [string, string[][]][] + ).reduce( + (acc, [key, [val]]) => ({ + ...acc, + [key]: val.map((v) => decodeURIComponent(v)) + }), + {} as Record + ); + ctx.request = new WebsocketNotificationRequest( trigger.getData_asU8(), getTraceContext(trigger.getTraceContext()), trigger.getWebsocket().getSocket(), trigger.getWebsocket().getEvent(), - trigger.getWebsocket().getConnectionid() + trigger.getWebsocket().getConnectionid(), + query, ); ctx.response = { @@ -662,13 +678,15 @@ export class WebsocketNotificationRequest extends AbstractRequest { public readonly socket: string; public readonly notificationType: WebsocketNotificationType; public readonly connectionId: string; + public readonly query: Record constructor( data: string | Uint8Array, traceContext: api.Context, socket: string, notificationType: WebsocketNotificationType, - connectionId: string + connectionId: string, + query: Record, ) { super(data, traceContext); @@ -676,6 +694,7 @@ export class WebsocketNotificationRequest extends AbstractRequest { this.socket = socket; this.notificationType = notificationType; this.connectionId = connectionId; + this.query = query; } } diff --git a/src/gen/proto/faas/v1/faas_pb.d.ts b/src/gen/proto/faas/v1/faas_pb.d.ts index 34f0a246..a49c8f8f 100644 --- a/src/gen/proto/faas/v1/faas_pb.d.ts +++ b/src/gen/proto/faas/v1/faas_pb.d.ts @@ -687,6 +687,8 @@ export class WebsocketTriggerContext extends jspb.Message { getConnectionid(): string; setConnectionid(value: string): void; + getQueryParamsMap(): jspb.Map; + clearQueryParamsMap(): void; serializeBinary(): Uint8Array; toObject(includeInstance?: boolean): WebsocketTriggerContext.AsObject; static toObject(includeInstance: boolean, msg: WebsocketTriggerContext): WebsocketTriggerContext.AsObject; @@ -702,6 +704,7 @@ export namespace WebsocketTriggerContext { socket: string, event: WebsocketEventMap[keyof WebsocketEventMap], connectionid: string, + queryParamsMap: Array<[string, QueryValue.AsObject]>, } } diff --git a/src/gen/proto/faas/v1/faas_pb.js b/src/gen/proto/faas/v1/faas_pb.js index 586137e3..b01e567d 100644 --- a/src/gen/proto/faas/v1/faas_pb.js +++ b/src/gen/proto/faas/v1/faas_pb.js @@ -5353,7 +5353,8 @@ proto.nitric.faas.v1.WebsocketTriggerContext.toObject = function(includeInstance var f, obj = { socket: jspb.Message.getFieldWithDefault(msg, 1, ""), event: jspb.Message.getFieldWithDefault(msg, 2, 0), - connectionid: jspb.Message.getFieldWithDefault(msg, 3, "") + connectionid: jspb.Message.getFieldWithDefault(msg, 3, ""), + queryParamsMap: (f = msg.getQueryParamsMap()) ? f.toObject(includeInstance, proto.nitric.faas.v1.QueryValue.toObject) : [] }; if (includeInstance) { @@ -5402,6 +5403,12 @@ proto.nitric.faas.v1.WebsocketTriggerContext.deserializeBinaryFromReader = funct var value = /** @type {string} */ (reader.readString()); msg.setConnectionid(value); break; + case 6: + var value = msg.getQueryParamsMap(); + reader.readMessage(value, function(message, reader) { + jspb.Map.deserializeBinary(message, reader, jspb.BinaryReader.prototype.readString, jspb.BinaryReader.prototype.readMessage, proto.nitric.faas.v1.QueryValue.deserializeBinaryFromReader, "", new proto.nitric.faas.v1.QueryValue()); + }); + break; default: reader.skipField(); break; @@ -5452,6 +5459,10 @@ proto.nitric.faas.v1.WebsocketTriggerContext.serializeBinaryToWriter = function( f ); } + f = message.getQueryParamsMap(true); + if (f && f.getLength() > 0) { + f.serializeBinary(6, writer, jspb.BinaryWriter.prototype.writeString, jspb.BinaryWriter.prototype.writeMessage, proto.nitric.faas.v1.QueryValue.serializeBinaryToWriter); + } }; @@ -5509,6 +5520,28 @@ proto.nitric.faas.v1.WebsocketTriggerContext.prototype.setConnectionid = functio }; +/** + * map query_params = 6; + * @param {boolean=} opt_noLazyCreate Do not create the map if + * empty, instead returning `undefined` + * @return {!jspb.Map} + */ +proto.nitric.faas.v1.WebsocketTriggerContext.prototype.getQueryParamsMap = function(opt_noLazyCreate) { + return /** @type {!jspb.Map} */ ( + jspb.Message.getMapField(this, 6, opt_noLazyCreate, + proto.nitric.faas.v1.QueryValue)); +}; + + +/** + * Clears values from the map. The map will be non-null. + * @return {!proto.nitric.faas.v1.WebsocketTriggerContext} returns this + */ +proto.nitric.faas.v1.WebsocketTriggerContext.prototype.clearQueryParamsMap = function() { + this.getQueryParamsMap().clear(); + return this;}; + + /** * Oneof group definitions for this message. Each group defines the field From d55c3b59ff4e5693b03724cea4c34542cf903965 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Wed, 19 Jul 2023 12:12:44 +1000 Subject: [PATCH 2/5] bump nitric version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fe83422e..8f2b57c0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@nitric/sdk", "description": "Nitric NodeJS client sdk", - "nitric": "v0.32.0-rc.3", + "nitric": "v0.32.0", "author": "Nitric ", "repository": "https://github.com/nitrictech/node-sdk", "main": "lib/index.js", From 6dffdf4850f32db19e161c81722680419e700f7f Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Wed, 19 Jul 2023 12:36:19 +1000 Subject: [PATCH 3/5] prettier --- src/faas/v0/context.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/faas/v0/context.ts b/src/faas/v0/context.ts index c8e4e7e2..ab3019a6 100644 --- a/src/faas/v0/context.ts +++ b/src/faas/v0/context.ts @@ -635,7 +635,7 @@ export class WebsocketNotificationContext extends TriggerContext< ).reduce( (acc, [key, [val]]) => ({ ...acc, - [key]: val.map((v) => decodeURIComponent(v)) + [key]: val.map((v) => decodeURIComponent(v)), }), {} as Record ); @@ -646,7 +646,7 @@ export class WebsocketNotificationContext extends TriggerContext< trigger.getWebsocket().getSocket(), trigger.getWebsocket().getEvent(), trigger.getWebsocket().getConnectionid(), - query, + query ); ctx.response = { @@ -678,7 +678,7 @@ export class WebsocketNotificationRequest extends AbstractRequest { public readonly socket: string; public readonly notificationType: WebsocketNotificationType; public readonly connectionId: string; - public readonly query: Record + public readonly query: Record; constructor( data: string | Uint8Array, @@ -686,7 +686,7 @@ export class WebsocketNotificationRequest extends AbstractRequest { socket: string, notificationType: WebsocketNotificationType, connectionId: string, - query: Record, + query: Record ) { super(data, traceContext); From 229f481cc3e6646fd69bf4163a065b7df2279a57 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Thu, 20 Jul 2023 13:18:30 +1000 Subject: [PATCH 4/5] use noopspanprocessor for local run. --- src/faas/v0/traceProvider.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/faas/v0/traceProvider.ts b/src/faas/v0/traceProvider.ts index 9e7f1db6..e84348f0 100644 --- a/src/faas/v0/traceProvider.ts +++ b/src/faas/v0/traceProvider.ts @@ -15,6 +15,7 @@ import { ConsoleSpanExporter, BatchSpanProcessor, NodeTracerProvider, + NoopSpanProcessor, } from '@opentelemetry/sdk-trace-node'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; @@ -51,13 +52,11 @@ const newTracerProvider = (): NodeTracerProvider => { tracerProvider: provider, }); - const traceExporter = localRun // If running locally - ? new ConsoleSpanExporter() - : new OTLPTraceExporter({ - url: 'http://localhost:4317', - }); - - const processor = new BatchSpanProcessor(traceExporter); + const processor = localRun + ? new NoopSpanProcessor() + : new BatchSpanProcessor(new OTLPTraceExporter({ + url: 'http://localhost:4317', + })); provider.addSpanProcessor(processor); provider.register(); From 6f917456fb5585f7aa94fb4cbab98ec5e46b40a9 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Thu, 20 Jul 2023 13:20:31 +1000 Subject: [PATCH 5/5] prettier --- src/faas/v0/traceProvider.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/faas/v0/traceProvider.ts b/src/faas/v0/traceProvider.ts index e84348f0..b97eff35 100644 --- a/src/faas/v0/traceProvider.ts +++ b/src/faas/v0/traceProvider.ts @@ -54,9 +54,11 @@ const newTracerProvider = (): NodeTracerProvider => { const processor = localRun ? new NoopSpanProcessor() - : new BatchSpanProcessor(new OTLPTraceExporter({ - url: 'http://localhost:4317', - })); + : new BatchSpanProcessor( + new OTLPTraceExporter({ + url: 'http://localhost:4317', + }) + ); provider.addSpanProcessor(processor); provider.register();