-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add pako gzip package * topic compression options * Message decompression * Added examples * docs(changeset): PUB-90 Add support for gzip compression/decompression * Update src/interface/publishOptions.ts * fix tests * review fix * Update src/message/gzip.ts * reduce coverage threshold
- Loading branch information
Showing
22 changed files
with
258 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@honestfoodcompany/pubsub': minor | ||
--- | ||
|
||
PUB-90 Add support for gzip compression/decompression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
id: Messages_compression | ||
title: Messages compression | ||
sidebar_position: 1 | ||
--- | ||
|
||
Framework supports gzip compression/decompression for messages. | ||
|
||
It uses [zlib port to js](https://github.com/nodeca/pako) for gzip and ungzip messages. | ||
|
||
## Compression | ||
|
||
[Topic compression option](../publishing/topics#compression) | ||
|
||
## Decompression | ||
|
||
`message.toJSON()` automatically checks if payload is gzip compressed and decompress it. | ||
|
||
Also, framework exports `isGzipCompressed(data: Buffer)` function to check if data payload is compressed. | ||
|
||
## Examples | ||
|
||
```ts title="/pubsub/subscriptions/compression.topic.example.sub.ts" | ||
import { SubscriberObject, isGzipCompressed } from '@honestfoodcompany/pubsub'; | ||
|
||
type Payload = any; | ||
|
||
const subscriber: SubscriberObject<Payload> = { | ||
topicName: 'compression.topic', | ||
subscriptionName: 'compression.topic.console-log.subscription', | ||
|
||
handleMessage: (message) => { | ||
console.log('is compressed', isGzipCompressed(message.data)); // true if payload is compressed | ||
console.log(message.toJSON()); // automatically decompress payload if it's compressed | ||
message.ack(); | ||
}, | ||
}; | ||
|
||
export default subscriber; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import CompressionTopic from '../topics/compression.topic'; | ||
|
||
// Run with: npx ts-node -r tsconfig-paths/register examples/typescript/scripts/compress-publish.ts | ||
|
||
const main = async () => { | ||
const topic = new CompressionTopic(); | ||
const messageId = await topic.publish({ testPayload: 'test' }); | ||
console.log('messageId', messageId); | ||
}; | ||
|
||
if (require.main === module) { | ||
void main(); | ||
} |
21 changes: 21 additions & 0 deletions
21
examples/typescript/subscriptions/compression.topic.console-log.sub.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SubscriberObject, isGzipCompressed } from '@honestfoodcompany/pubsub'; | ||
interface Payload { | ||
testPayload: string; | ||
} | ||
|
||
const subscriber: SubscriberObject<Payload> = { | ||
topicName: 'compression.topic', | ||
subscriptionName: 'compression.topic.console-log.subscription', | ||
description: 'Will console log messages published on compression.topic', | ||
|
||
handleMessage: (message) => { | ||
console.log('is compressed', isGzipCompressed(message.data)); | ||
|
||
console.log(message.toJSON(), typeof message.toJSON()); | ||
const x = message.toJSON(); | ||
console.log(x?.testPayload); | ||
message.ack(); | ||
}, | ||
}; | ||
|
||
export default subscriber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
Topic, | ||
Payload as BasePayload, | ||
TopicOptions, | ||
} from '@honestfoodcompany/pubsub'; | ||
|
||
/** | ||
* Defining a topic and payload: | ||
*/ | ||
export interface Payload extends BasePayload { | ||
testPayload: any; | ||
} | ||
|
||
export default class CompressionTopic extends Topic<Payload> { | ||
static readonly topicName = 'compression.topic'; | ||
|
||
public options: TopicOptions = { | ||
enableGZipCompression: true, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
"cli-table": "0.3.6", | ||
"dotenv": "10.0.0", | ||
"find-config": "1.0.0", | ||
"pako": "2.1.0", | ||
"wrap-ansi": "7.0.0", | ||
"yargs": "17.2.1" | ||
}, | ||
|
@@ -55,6 +56,7 @@ | |
"@types/cli-table": "^0.3.0", | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^16", | ||
"@types/pako": "^2", | ||
"@types/react": "^17.0.34", | ||
"@types/wrap-ansi": "^8.0.1", | ||
"@types/yargs": "^17.0.5", | ||
|
@@ -108,4 +110,4 @@ | |
} | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* gzip files contain a 10-byte header, containing a: | ||
* - magic number (1f 8b) | ||
* - the compression method (08 for DEFLATE) | ||
* - 1-byte of header flags, a 4-byte timestamp, compression flags and the operating system ID. | ||
* | ||
* @see https://en.wikipedia.org/wiki/Gzip#File_format | ||
*/ | ||
export const isGzipCompressed = (buf: Buffer): boolean => { | ||
return buf[0] === 0x1f && buf[1] === 0x8b && buf[2] === 0x08; | ||
}; |
Oops, something went wrong.