-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(worker): expose TextEncoder and TextDecoder (#1562)
Co-authored-by: James Watkins-Harvey <[email protected]>
- Loading branch information
1 parent
d6a4992
commit 147ec50
Showing
7 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/test/src/test-worker-exposes-textencoderdecoder.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,36 @@ | ||
import test from 'ava'; | ||
import { v4 as uuid4 } from 'uuid'; | ||
import { Client } from '@temporalio/client'; | ||
import { RUN_INTEGRATION_TESTS, Worker } from './helpers'; | ||
import { defaultOptions } from './mock-native-worker'; | ||
import { textEncoderDecoder, textEncoderDecoderFromImport } from './workflows'; | ||
|
||
if (RUN_INTEGRATION_TESTS) { | ||
test('Worker runtime exposes TextEncoder and TextDecoder as globals', async (t) => { | ||
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' }); | ||
const client = new Client(); | ||
const result = await worker.runUntil( | ||
client.workflow.execute(textEncoderDecoder, { | ||
args: ['a string that will be encoded and decoded'], | ||
taskQueue: 'test-worker-exposes-textencoderdecoder', | ||
workflowId: uuid4(), | ||
workflowExecutionTimeout: '5s', | ||
}) | ||
); | ||
t.is(result, 'a string that will be encoded and decoded'); | ||
}); | ||
|
||
test('Worker runtime exposes TextEncoder and TextDecoder as overrided import of util', async (t) => { | ||
const worker = await Worker.create({ ...defaultOptions, taskQueue: 'test-worker-exposes-textencoderdecoder' }); | ||
const client = new Client(); | ||
const result = await worker.runUntil( | ||
client.workflow.execute(textEncoderDecoderFromImport, { | ||
args: ['a string that will be encoded and decoded'], | ||
taskQueue: 'test-worker-exposes-textencoderdecoder', | ||
workflowId: uuid4(), | ||
workflowExecutionTimeout: '5s', | ||
}) | ||
); | ||
t.is(result, 'a string that will be encoded and decoded'); | ||
}); | ||
} |
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,18 @@ | ||
import { TextEncoder as TextEncoderFromImport, TextDecoder as TextDecoderFromImport } from 'util'; | ||
|
||
export async function textEncoderDecoder(text: string): Promise<string> { | ||
// we don't import these - they are exposed as globals | ||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
const encoded = encoder.encode(text); | ||
const decoded = decoder.decode(encoded); | ||
return decoded; | ||
} | ||
|
||
export async function textEncoderDecoderFromImport(text: string): Promise<string> { | ||
const encoder = new TextEncoderFromImport(); | ||
const decoder = new TextDecoderFromImport(); | ||
const encoded = encoder.encode(text); | ||
const decoded = decoder.decode(encoded); | ||
return decoded; | ||
} |
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,3 @@ | ||
/* eslint-disable import/unambiguous */ | ||
// Only expose the TextEncoder and TextDecoder APIs | ||
module.exports = { TextEncoder, TextDecoder }; |
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