wirejs-deploy-amplify-basic 0.0.105-realtime → 0.0.107-realtime

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,7 +51,11 @@ export const handler = async (event: AuthorizationRequest): Promise<Authorizatio
51
51
  * 4. Must be for the correct channel
52
52
  */
53
53
  try {
54
- console.log('Authorization event:', JSON.stringify(event, null, 2));
54
+ console.log(
55
+ 'Authorization request:',
56
+ event.requestContext.operation,
57
+ event.requestContext.channel
58
+ );
55
59
 
56
60
  if (event.requestContext.operation === 'EVENT_PUBLISH') {
57
61
  throw new Error('Publish not permitted');
@@ -66,10 +70,15 @@ export const handler = async (event: AuthorizationRequest): Promise<Authorizatio
66
70
  new TextEncoder().encode(await getSecret(event.requestContext.channelNamespaceName))
67
71
  );
68
72
 
69
- if (event.requestContext.channel && decoded.payload.channel !== event.requestContext.channel) {
70
- throw new Error(`Channel mismatch: expected ${event.requestContext.channel}, got ${decoded.payload.channel}`);
73
+ // channel from context gets prefixed with a slash
74
+ if (event.requestContext.channel
75
+ && `/${decoded.payload.channel}` !== event.requestContext.channel
76
+ ) {
77
+ throw new Error(`Channel mismatch: Token is for "/${decoded.payload.channel}", authorization request is for "${event.requestContext.channel}".`);
71
78
  }
72
79
 
80
+ console.log('Authorized.');
81
+
73
82
  // Default TTL from AppSync is 5 minutes according to construct docstring.
74
83
  return {
75
84
  isAuthorized: true,
@@ -3,6 +3,6 @@
3
3
  "dependencies": {
4
4
  "jsdom": "^25.0.1",
5
5
  "wirejs-dom": "^1.0.41",
6
- "wirejs-resources": "^0.1.73-realtime"
6
+ "wirejs-resources": "^0.1.75-realtime"
7
7
  }
8
8
  }
@@ -8,6 +8,6 @@ export declare class RealtimeService<T = any> extends Resource {
8
8
  * The address the client will need to connect to.
9
9
  */
10
10
  get address(): string;
11
- publish(channel: string, data: T | T[]): Promise<void>;
11
+ publish(channel: string, events: T[]): Promise<any>;
12
12
  getStream(channel: string): Promise<any>;
13
13
  }
@@ -52,23 +52,25 @@ export class RealtimeService extends Resource {
52
52
  get address() {
53
53
  return `wss://${process.env.REALTIME_WS_DOMAIN}/event/realtime`;
54
54
  }
55
- async publish(channel, data) {
55
+ async publish(channel, events) {
56
56
  this.#validateChannelName(channel);
57
- console.log('Publishing to channels:', channel);
58
- const events = Array.isArray(data) ? data : [data];
57
+ console.log('Attempting to publish to channel:', channel);
59
58
  // AppSync allows batches of no more than 5. Hence, if we have more than 5,
60
59
  // we need to perform batching on our end.
61
60
  if (events.length > 5) {
61
+ // console.log(`More than 5 events received. Batching...`);
62
+ let results = [];
62
63
  let i = 0;
63
64
  while (i < events.length) {
64
65
  const batch = events.slice(i, i + 5);
65
- await this.publish(channel, batch);
66
+ results.push(await this.publish(channel, batch));
66
67
  i += 5;
67
68
  }
68
- return;
69
+ return results;
69
70
  }
70
71
  // TODO: Utility for making SigV4 requests a little more concise.
71
72
  const credentials = await defaultProvider()();
73
+ ;
72
74
  const signer = new SignatureV4({
73
75
  service: 'appsync',
74
76
  region: process.env.AWS_REGION || 'us-east-1',
@@ -98,16 +100,16 @@ export class RealtimeService extends Resource {
98
100
  headers: signedRequest.headers,
99
101
  body: signedRequest.body,
100
102
  });
101
- await response.text();
102
- console.log('Response from publish:', response.status, response.statusText);
103
+ console.log(`AppSync response status:`, response.status);
103
104
  if (!response.ok) {
104
105
  throw new Error(`Failed to publish to channel: ${response.statusText}`);
105
106
  }
107
+ return response.json();
106
108
  }
107
109
  async getStream(channel) {
108
110
  this.#validateChannelName(channel);
109
111
  const channelString = `${this.#namespace}/${channel}`;
110
- const payload = { channel: `/${channelString}` };
112
+ const payload = { channel: channelString };
111
113
  const jwt = await new jose.SignJWT(payload)
112
114
  .setProtectedHeader({ alg: 'HS256' })
113
115
  .setIssuedAt()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-deploy-amplify-basic",
3
- "version": "0.0.105-realtime",
3
+ "version": "0.0.107-realtime",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -41,7 +41,7 @@
41
41
  "recursive-copy": "^2.0.14",
42
42
  "rimraf": "^6.0.1",
43
43
  "wirejs-dom": "^1.0.41",
44
- "wirejs-resources": "^0.1.73-realtime"
44
+ "wirejs-resources": "^0.1.75-realtime"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@aws-amplify/backend": "^1.14.0",