util#TextEncoder TypeScript Examples

The following examples show how to use util#TextEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: server.ts    From sanctuary with Do What The F*ck You Want To Public License 6 votes vote down vote up
app.get('/api/v1/players', (req, res) => {
  let game = getGame();

  if (!game) {
    res.send(JSON.stringify({ type: "error", message: "No game active." }));
  } else {
    let clients: { clientIPHash: string, playerName: string, playerID: number }[] = [];

    for (let client of game.clients) {
      clients.push(
        {
          clientIPHash: arrayBufferToHex(SHA256(new TextEncoder().encode(client.ip))),
          playerName: client.player?.name || "unknown",
          playerID: client.player?.id || -1
        }
      );
    }

    res.send(JSON.stringify({ type: "success", clients: clients }));
  }
});
Example #2
Source File: Exif.ts    From wa-sticker-formatter with MIT License 6 votes vote down vote up
build = (): Buffer => {
        const data = JSON.stringify(this.data)
        const exif = Buffer.concat([
            Buffer.from([
                0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x16, 0x00, 0x00, 0x00
            ]),
            Buffer.from(data, 'utf-8')
        ])
        exif.writeUIntLE(new TextEncoder().encode(data).length, 14, 4)
        return exif
    }
Example #3
Source File: tx.ts    From algo-builder with Apache License 2.0 6 votes vote down vote up
describe("Note in TxParams", () => {
	const encoder = new TextEncoder();
	const note = "Hello Algob!";
	const noteb64 = "asdisaddas";

	it("Both notes given", () => {
		const result = webTx.encodeNote(note, noteb64);
		assert.deepEqual(result, encoder.encode(noteb64), "noteb64 not encoded");
	});

	it("Only note given", () => {
		const result = webTx.encodeNote(note, undefined);
		assert.deepEqual(result, encoder.encode(note), "note not encoded");
	});

	it("Only noteb64 given", () => {
		const result = webTx.encodeNote(undefined, noteb64);
		assert.deepEqual(result, encoder.encode(noteb64), "noteb64 not encoded");
	});
});
Example #4
Source File: utils.ts    From aws-transcribe with MIT License 6 votes vote down vote up
encoder = new TextEncoder()
Example #5
Source File: serializer.ts    From vscode-sql-notebook with MIT License 6 votes vote down vote up
async serializeNotebook(
    data: vscode.NotebookData,
    _token: vscode.CancellationToken
  ): Promise<Uint8Array> {
    return new TextEncoder().encode(
      data.cells
        .map(({ value, kind }) =>
          kind === vscode.NotebookCellKind.Code
            ? value
            : `/*markdown\n${value}\n*/`
        )
        .join('\n\n')
    );
  }
Example #6
Source File: EosBlockchainService.ts    From aloxide with Apache License 2.0 6 votes vote down vote up
constructor(config: NetworkConfig) {
    super(config);
    const signatureProvider = new JsSignatureProvider([]);
    const rpc = new JsonRpc(this.url(), { fetch });

    this.client = new Api({
      rpc,
      signatureProvider,
      textEncoder: new TextEncoder(),
      textDecoder: new TextDecoder(),
    });
  }
Example #7
Source File: EosBlockchainModel.ts    From aloxide with Apache License 2.0 6 votes vote down vote up
constructor(
    name: string,
    url: string,
    actions: BlockchainAction[],
    contract: string,
    account?: BlockchainAccount,
  ) {
    super(name, url, actions, contract, account);

    let keys = [];
    if (this.account) {
      keys = [this.account.privateKey];
    }
    const signatureProvider = new JsSignatureProvider(keys);
    const rpc = new JsonRpc(url, { fetch });

    this.client = new Api({
      rpc,
      signatureProvider,
      textEncoder: new TextEncoder(),
      textDecoder: new TextDecoder(),
    });
  }
Example #8
Source File: constants.ts    From xapi with MIT License 5 votes vote down vote up
testAttachmentArrayBuffer: ArrayBuffer = new TextEncoder().encode(
  testAttachmentContent
)
Example #9
Source File: socket.ts    From ib with MIT License 5 votes vote down vote up
/**
   * Encode a string to a UTF8 byte array.
   */
  private stringToUTF8Array(val: string): number[] {
    return Array.from(new TextEncoder().encode(val));
  }
Example #10
Source File: helpers.ts    From livepeer-com with MIT License 5 votes vote down vote up
export async function hash(password: string, salt: string) {
  let saltBuffer;
  if (salt) {
    saltBuffer = fromHexString(salt);
  } else {
    saltBuffer = crypto.getRandomValues(new Uint8Array(8));
  }

  var encoder = new TextEncoder();
  var passphraseKey = encoder.encode(password);

  // You should firstly import your passphrase Uint8array into a CryptoKey
  const key = await crypto.subtle.importKey(
    "raw",
    passphraseKey,
    { name: "PBKDF2" },
    false,
    ["deriveBits", "deriveKey"]
  );
  const webKey = await crypto.subtle.deriveKey(
    {
      name: "PBKDF2",
      salt: saltBuffer,
      // don't get too ambitious, or at least remember
      // that low-power phones will access your app
      iterations: ITERATIONS,
      hash: "SHA-256",
    },
    key,

    // Note: for this demo we don't actually need a cipher suite,
    // but the api requires that it must be specified.
    // For AES the length required to be 128 or 256 bits (not bytes)
    { name: "AES-CBC", length: 256 },

    // Whether or not the key is extractable (less secure) or not (more secure)
    // when false, the key can only be passed as a web crypto object, not inspected
    true,

    // this web crypto object will only be allowed for these functions
    ["encrypt", "decrypt"]
  );
  const buffer = await crypto.subtle.exportKey("raw", webKey);

  const outKey = bytesToHexString(new Uint8Array(buffer));
  const outSalt = bytesToHexString(saltBuffer);
  return [outKey, outSalt];
}
Example #11
Source File: dummy.ts    From aloxide with Apache License 2.0 5 votes vote down vote up
api = new Api({
  rpc,
  signatureProvider,
  textDecoder: new TextDecoder(),
  textEncoder: new TextEncoder(),
})
Example #12
Source File: setupJest.ts    From erda-ui with GNU Affero General Public License v3.0 5 votes vote down vote up
customGlobal.TextEncoder = TextEncoder;
Example #13
Source File: plugin.ts    From vs-code-conan with MIT License 5 votes vote down vote up
async writeFile(filepath: string, content: string): Promise<void>{
        const finalUri = Uri.file(filepath);
        let enc = new TextEncoder(); 
        await workspace.fs.writeFile(finalUri,enc.encode(content));
    }
Example #14
Source File: shared.ts    From ue4-intellisense-fixes with MIT License 5 votes vote down vote up
/**
 * Synchronously reads string from file
 * 
 * @param path 
 * @param encoding Default 'utf-8'
 * 
 * @returns returns undefined on failure
 * 
 * @logs console.error Error.code
 */
/* export function readStringFromFileSync(path: string, encoding: BufferEncoding = consts.ENCODING_UTF_8): string | undefined {

    try {
        
        return fsSync.readFileSync(path, encoding ) as string;
        
    }
    catch (error) {
        if(error instanceof Error){ 
            console.error(`Error reading ${path}. Message: ${error.message}`);
        }
        return undefined;
    }
} */


/**
 * Asynchronously write json to file
 * 
 * @param path 
 * @param data Any data except string will use JSON.stringify
 * @param encoding Default 'utf-8'
 * @logs console.error Error.code
 */
export async function writeJsonOrStringToFile(path: string, data: any, encoding: BufferEncoding = consts.ENCODING_UTF_8,
    spacing: number = consts.JSON_SPACING) {
    try {
        const writeData = typeof data === "string" ? data : JSON.stringify(data, undefined, spacing);
        //await fsAsync.writeFile(path, writeData, encoding);
        const textEnc = new TextEncoder();
        const fileBuffer = Uint8Array.from(textEnc.encode(writeData));
        await vscode.workspace.fs.writeFile(vscode.Uri.file(path), fileBuffer);
        return;
    }
    catch (error) {
        if (error instanceof Error) {
            console.error(`Error writing json file. Message: ${error.message}`);
        }
        return;
    }

}
Example #15
Source File: DryRunContext.test.tsx    From backstage with Apache License 2.0 5 votes vote down vote up
window.TextEncoder = TextEncoder;
Example #16
Source File: setupTests.ts    From backstage with Apache License 2.0 5 votes vote down vote up
// Also used in browser-based APIs for hashing.
Object.defineProperty(global.self, 'TextEncoder', {
  value: TextEncoder,
});
Example #17
Source File: bee-class.spec.ts    From bee-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
describe('Bee class', () => {
  const BEE_URL = beeUrl()
  const BEE_KY = beeKy()
  const BEE_PEER_URL = beePeerUrl()
  const BEE_DEBUG_PEER_URL = beePeerDebugUrl()
  const bee = new Bee(BEE_URL)
  const beePeer = new Bee(BEE_PEER_URL)

  it('should strip trailing slash', () => {
    const bee = new Bee('http://127.0.0.1:1633/')
    expect(bee.url).toEqual('http://127.0.0.1:1633')
  })

  describe('chunk', () => {
    it('should upload and download chunk', async () => {
      const content = randomByteArray(100)

      const reference = await bee.uploadChunk(getPostageBatch(), content)
      const downloadedChunk = await bee.downloadChunk(reference)

      expect(downloadedChunk).toEqual(content)
    })

    it('should upload and download chunk with direct upload', async () => {
      const content = randomByteArray(100)

      const reference = await bee.uploadChunk(getPostageBatch(), content, { deferred: false })
      const downloadedChunk = await bee.downloadChunk(reference)

      expect(downloadedChunk).toEqual(content)
    })
  })

  describe('files', () => {
    it('should work with files', async () => {
      const content = new Uint8Array([1, 2, 3])
      const name = 'hello.txt'
      const contentType = 'text/html'

      const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType })
      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data).toEqual(content)
    })

    it('should work with files and CIDs', async () => {
      const content = new Uint8Array([1, 2, 3])
      const name = 'hello.txt'
      const contentType = 'text/html'

      const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType })
      const file = await bee.downloadFile(result.cid)

      expect(file.name).toEqual(name)
      expect(file.data).toEqual(content)
    })

    it('should work with files and direct upload', async () => {
      const content = new Uint8Array([1, 2, 3])
      const name = 'hello.txt'
      const contentType = 'text/html'

      const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType, deferred: false })
      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data).toEqual(content)
    })

    it('should work with files and tags', async () => {
      const tag = await bee.createTag()

      // Should fit into 4 chunks
      const content = randomByteArray(13000)
      const name = 'hello.txt'
      const contentType = 'text/html'

      const result = await bee.uploadFile(getPostageBatch(), content, name, { contentType, tag: tag.uid })
      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data).toEqual(content)

      const retrievedTag = await bee.retrieveTag(tag)
      expect(retrievedTag.total).toEqual(8)
    })

    it('should work with file object', async () => {
      const content = new Uint8Array([1, 2, 3])
      const name = 'hello.txt'
      const type = 'text/plain'
      const file = {
        arrayBuffer: () => content,
        name,
        type,
      } as unknown as File

      const result = await bee.uploadFile(getPostageBatch(), file)
      const downloadedFile = await bee.downloadFile(result.reference)

      expect(downloadedFile.data).toEqual(content)
      expect(downloadedFile.name).toEqual(name)
      expect(downloadedFile.contentType).toEqual(type)
    })

    it('should work with file object and name overridden', async () => {
      const content = new Uint8Array([1, 2, 3])
      const name = 'hello.txt'
      const file = {
        arrayBuffer: () => content,
        name,
      } as unknown as File
      const nameOverride = 'hello-override.txt'

      const result = await bee.uploadFile(getPostageBatch(), file, nameOverride)
      const downloadedFile = await bee.downloadFile(result.reference)

      expect(downloadedFile.data).toEqual(content)
      expect(downloadedFile.name).toEqual(nameOverride)
    })

    it('should work with file object and content-type overridden', async () => {
      const content = new Uint8Array([1, 2, 3])
      const file = {
        arrayBuffer: () => content,
        name: 'hello.txt',
        type: 'text/plain',
      } as unknown as File
      const contentTypeOverride = 'text/plain+override'

      const result = await bee.uploadFile(getPostageBatch(), file, undefined, { contentType: contentTypeOverride })
      const downloadedFile = await bee.downloadFile(result.reference)

      expect(downloadedFile.data).toEqual(content)
      expect(downloadedFile.contentType).toEqual(contentTypeOverride)
    })

    it('should work with NodeJS readable', async () => {
      const readable = Readable.from([new TextEncoder().encode('hello '), new TextEncoder().encode('world')])
      const name = 'hello.txt'
      const contentType = 'text/plain'

      const result = await bee.uploadFile(getPostageBatch(), readable, name, { contentType })
      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data.text()).toEqual('hello world')
    })

    it('should work with WHATWG readable-stream', async () => {
      const readable = createReadableStream([new TextEncoder().encode('hello '), new TextEncoder().encode('world')])
      const name = 'hello.txt'
      const contentType = 'text/plain'

      const result = await bee.uploadFile(getPostageBatch(), readable, name, { contentType })
      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data.text()).toEqual('hello world')
    }, 1000000)

    it('should work with readable and tags', async () => {
      const tag = await bee.createTag()

      const readable = createRandomNodeReadable(13000)
      const name = 'hello.txt'
      const contentType = 'text/plain'

      const result = await bee.uploadFile(getPostageBatch(), readable, name, {
        contentType,
        tag: tag.uid,
      })

      const file = await bee.downloadFile(result.reference)

      expect(file.name).toEqual(name)
      expect(file.data.length).toEqual(13000)

      const retrievedTag = await bee.retrieveTag(tag)
      expect(retrievedTag.total).toEqual(8)
    })
  })

  describe('collections', () => {
    it('should work with directory with unicode filenames', async () => {
      const result = await bee.uploadFilesFromDirectory(getPostageBatch(), './test/data')

      expect(result.reference.length).toEqual(REFERENCE_HEX_LENGTH)
    })

    it('should work with directory with unicode filenames and direct upload', async () => {
      const result = await bee.uploadFilesFromDirectory(getPostageBatch(), './test/data', { deferred: false })

      expect(result.reference.length).toEqual(REFERENCE_HEX_LENGTH)
    })

    it('should upload collection', async () => {
      const directoryStructure: Collection<Uint8Array> = [
        {
          path: '0',
          data: new TextEncoder().encode('hello-world'),
        },
      ]

      const result = await bee.uploadCollection(getPostageBatch(), directoryStructure)
      const file = await bee.downloadFile(result.reference, directoryStructure[0].path)

      expect(file.name).toEqual(directoryStructure[0].path)
      expect(file.data.text()).toEqual('hello-world')
    })

    it('should upload collection with CIDs support', async () => {
      const directoryStructure: Collection<Uint8Array> = [
        {
          path: '0',
          data: new TextEncoder().encode('hello-CID-world'),
        },
      ]

      const result = await bee.uploadCollection(getPostageBatch(), directoryStructure)
      const file = await bee.downloadFile(result.cid, directoryStructure[0].path)

      expect(file.name).toEqual(directoryStructure[0].path)
      expect(file.data.text()).toEqual('hello-CID-world')
    })
  })

  describe('tags', () => {
    it('should list tags', async () => {
      const originalTags = await bee.getAllTags({ limit: 1000 })
      const createdTag = await bee.createTag()
      const updatedTags = await bee.getAllTags({ limit: 1000 })

      expect(updatedTags.length - originalTags.length).toEqual(1)
      expect(originalTags.find(tag => tag.uid === createdTag.uid)).toBeFalsy()
      expect(updatedTags.find(tag => tag.uid === createdTag.uid)).toBeTruthy()
    })

    it('should retrieve previously created empty tag', async () => {
      const tag = await bee.createTag()
      const tag2 = await bee.retrieveTag(tag)

      expect(tag).toEqual(tag2)
    })

    it('should delete tag', async () => {
      const createdTag = await bee.createTag()
      const originalTags = await bee.getAllTags({ limit: 1000 })
      expect(originalTags.find(tag => tag.uid === createdTag.uid)).toBeTruthy()

      await bee.deleteTag(createdTag)
      const updatedTags = await bee.getAllTags({ limit: 1000 })

      expect(updatedTags.length - originalTags.length).toEqual(-1)
      expect(updatedTags.find(tag => tag.uid === createdTag.uid)).toBeFalsy()
    })
  })

  describe('pinning', () => {
    it('should list all pins', async () => {
      const content = new Uint8Array([1, 2, 3])
      const result = await bee.uploadFile(getPostageBatch(), content)

      await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong

      const pinnedChunks = await bee.getAllPins()
      expect(pinnedChunks).toBeType('array')
      expect(pinnedChunks.includes(result.reference)).toBeTruthy()
    })

    it('should get pinning status', async () => {
      const content = randomByteArray(16, Date.now())
      const result = await bee.uploadFile(getPostageBatch(), content, 'test', {
        pin: false,
      })

      const statusBeforePinning = bee.getPin(result.reference)
      await expect(statusBeforePinning).rejects.toThrowError('Not Found')

      await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong

      const statusAfterPinning = await bee.getPin(result.reference)
      expect(statusAfterPinning).toHaveProperty('reference', result.reference)
    })

    it('should pin and unpin files', async () => {
      const content = new Uint8Array([1, 2, 3])

      const result = await bee.uploadFile(getPostageBatch(), content)

      await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
      await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
    })

    it('should pin and unpin collection from directory', async () => {
      const path = './test/data/'
      const result = await bee.uploadFilesFromDirectory(getPostageBatch(), path)

      await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
      await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
    })

    it('should pin and unpin data', async () => {
      const content = new Uint8Array([1, 2, 3])

      const result = await bee.uploadData(getPostageBatch(), content)

      await bee.pin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
      await bee.unpin(result.reference) // Nothing is asserted as nothing is returned, will throw error if something is wrong
    })
  })

  describe('stewardship', () => {
    it('should reupload pinned data', async () => {
      const content = randomByteArray(16, Date.now())
      const result = await bee.uploadData(getPostageBatch(), content, { pin: true })

      await sleep(10)
      await bee.reuploadPinnedData(result.reference) // Does not return anything, but will throw exception if something is going wrong
    })

    it(
      'should check if reference is retrievable',
      async () => {
        const content = randomByteArray(16, Date.now())
        const result = await bee.uploadData(getPostageBatch(), content, { pin: true })

        await sleep(10)
        await expect(bee.isReferenceRetrievable(result.reference)).resolves.toEqual(true)

        // Reference that has correct form, but should not exist on the network
        await expect(
          bee.isReferenceRetrievable('ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4332'),
        ).resolves.toEqual(false)
      },
      ERR_TIMEOUT,
    )
  })

  describe('pss', () => {
    it(
      'should send and receive data',
      async () => {
        return new Promise<void>((resolve, reject) => {
          ;(async () => {
            const topic = 'bee-class-topic'
            const message = new Uint8Array([1, 2, 3])
            const beeDebug = new BeeDebug(beeDebugUrl())

            bee.pssReceive(topic).then(receivedMessage => {
              expect(receivedMessage).toEqual(message)
              resolve()
            })

            const { overlay } = await beeDebug.getNodeAddresses()
            await beePeer.pssSend(getPostageBatch(BEE_DEBUG_PEER_URL), topic, makeTestTarget(overlay), message)
          })().catch(reject)
        })
      },
      PSS_TIMEOUT,
    )

    it(
      'should send and receive data with public key',
      async () => {
        return new Promise<void>((resolve, reject) => {
          // Jest does not allow use `done` and return Promise so this wrapper work arounds that.
          ;(async () => {
            const topic = 'bee-class-topic-publickey'
            const message = new Uint8Array([1, 2, 3])
            const beeDebug = new BeeDebug(beeDebugUrl())

            bee.pssReceive(topic).then(receivedMessage => {
              expect(receivedMessage).toEqual(message)
              resolve()
            })

            const { overlay, pssPublicKey } = await beeDebug.getNodeAddresses()
            await beePeer.pssSend(
              getPostageBatch(BEE_DEBUG_PEER_URL),
              topic,
              makeTestTarget(overlay),
              message,
              pssPublicKey,
            )
          })().catch(reject)
        })
      },
      PSS_TIMEOUT,
    )

    it(
      'should subscribe to topic',
      async () => {
        return new Promise<void>((resolve, reject) => {
          let subscription: PssSubscription
          ;(async () => {
            const topic = 'bee-class-subscribe-topic'
            const message = new Uint8Array([1, 2, 3])
            const beeDebug = new BeeDebug(beeDebugUrl())

            subscription = bee.pssSubscribe(topic, {
              onMessage: receivedMessage => {
                // without cancel jest complains for leaking handles and may hang
                subscription?.cancel()

                expect(receivedMessage).toEqual(message)
                resolve()
              },
              onError: e => {
                throw e
              },
            })

            const { overlay } = await beeDebug.getNodeAddresses()
            await beePeer.pssSend(getPostageBatch(BEE_DEBUG_PEER_URL), topic, makeTestTarget(overlay), message)
          })().catch(e => {
            // without cancel jest complains for leaking handles and may hang
            subscription?.cancel()
            reject(e)
          })
        })
      },
      PSS_TIMEOUT,
    )

    it('should time out', async () => {
      const topic = 'bee-class-receive-timeout'

      await expect(bee.pssReceive(topic, 1)).rejects.toThrow('pssReceive timeout')
    })
  })

  describe('feeds', () => {
    const owner = testIdentity.address
    const signer = testIdentity.privateKey

    it(
      'should write two updates',
      async () => {
        const topic = randomByteArray(32, Date.now())

        const feed = bee.makeFeedWriter('sequence', topic, signer)
        const referenceZero = makeBytes(32) // all zeroes

        await feed.upload(getPostageBatch(), referenceZero)
        const firstUpdateReferenceResponse = await feed.download()

        expect(firstUpdateReferenceResponse.reference).toEqual(bytesToHex(referenceZero))
        expect(firstUpdateReferenceResponse.feedIndex).toEqual('0000000000000000')

        const referenceOne = new Uint8Array([...new Uint8Array([1]), ...new Uint8Array(31)]) as BytesReference

        await feed.upload(getPostageBatch(), referenceOne)
        const secondUpdateReferenceResponse = await feed.download()

        expect(secondUpdateReferenceResponse.reference).toEqual(bytesToHex(referenceOne))
        expect(secondUpdateReferenceResponse.feedIndex).toEqual('0000000000000001')
        // TODO the timeout was increased because this test is flaky
        //  most likely there is an issue with the lookup
        //  https://github.com/ethersphere/bee/issues/1248#issuecomment-786588911
      },
      FEED_TIMEOUT,
    )

    it(
      'should get specific feed update',
      async () => {
        const topic = randomByteArray(32, Date.now())

        const feed = bee.makeFeedWriter('sequence', topic, signer)
        const referenceZero = makeBytes(32) // all zeroes

        await feed.upload(getPostageBatch(), referenceZero)

        const firstLatestUpdate = await feed.download()
        expect(firstLatestUpdate.reference).toEqual(bytesToHex(referenceZero))
        expect(firstLatestUpdate.feedIndex).toEqual('0000000000000000')

        const referenceOne = new Uint8Array([...new Uint8Array([1]), ...new Uint8Array(31)]) as BytesReference
        await feed.upload(getPostageBatch(), referenceOne)

        const secondLatestUpdate = await feed.download()
        expect(secondLatestUpdate.reference).toEqual(bytesToHex(referenceOne))
        expect(secondLatestUpdate.feedIndex).toEqual('0000000000000001')

        const referenceTwo = new Uint8Array([
          ...new Uint8Array([1]),
          ...new Uint8Array([1]),
          ...new Uint8Array(30),
        ]) as BytesReference
        await feed.upload(getPostageBatch(), referenceTwo)

        const thirdLatestUpdate = await feed.download()
        expect(thirdLatestUpdate.reference).toEqual(bytesToHex(referenceTwo))
        expect(thirdLatestUpdate.feedIndex).toEqual('0000000000000002')

        const sendBackFetchedUpdate = await feed.download({ index: '0000000000000001' })
        expect(sendBackFetchedUpdate.reference).toEqual(bytesToHex(referenceOne))
        expect(sendBackFetchedUpdate.feedIndex).toEqual('0000000000000001')
      },
      FEED_TIMEOUT,
    )
    it(
      'should fail fetching non-existing index',
      async () => {
        const topic = randomByteArray(32, Date.now())

        const feed = bee.makeFeedWriter('sequence', topic, signer)
        const referenceZero = makeBytes(32) // all zeroes
        await feed.upload(getPostageBatch(), referenceZero)

        const firstLatestUpdate = await feed.download()
        expect(firstLatestUpdate.reference).toEqual(bytesToHex(referenceZero))
        expect(firstLatestUpdate.feedIndex).toEqual('0000000000000000')

        try {
          await feed.download({ index: '0000000000000001' })
          throw new Error('Should fail')
        } catch (err) {
          const e = err as BeeResponseError

          expect(e.status).toEqual(404)
          expect(e.responseBody).toContain('chunk not found')
        }
      },
      FEED_TIMEOUT,
    )

    it(
      'create feeds manifest and retrieve the data',
      async () => {
        const topic = randomByteArray(32, Date.now())

        const directoryStructure: Collection<Uint8Array> = [
          {
            path: 'index.html',
            data: new TextEncoder().encode('some data'),
          },
        ]
        const cacResult = await bzz.uploadCollection(BEE_KY, directoryStructure, getPostageBatch())

        const feed = bee.makeFeedWriter('sequence', topic, signer)
        await feed.upload(getPostageBatch(), cacResult.reference)
        const manifestReference = await bee.createFeedManifest(getPostageBatch(), 'sequence', topic, owner)

        expect(typeof manifestReference).toBe('string')

        // this calls /bzz endpoint that should resolve the manifest and the feed returning the latest feed's content
        const file = await bee.downloadFile(manifestReference, 'index.html')
        expect(new TextDecoder().decode(file.data)).toEqual('some data')
      },
      FEED_TIMEOUT,
    )

    describe('isFeedRetrievable', () => {
      const existingTopic = randomByteArray(32, Date.now())
      const updates: { index: string; reference: BytesReference }[] = [
        { index: '0000000000000000', reference: makeBytes(32) },
        { index: '0000000000000001', reference: Uint8Array.from([1, ...makeBytes(31)]) as BytesReference },
        { index: '0000000000000002', reference: Uint8Array.from([1, 1, ...makeBytes(30)]) as BytesReference },
      ]

      beforeAll(async () => {
        const feed = bee.makeFeedWriter('sequence', existingTopic, signer)

        await feed.upload(getPostageBatch(), updates[0].reference)
        await feed.upload(getPostageBatch(), updates[1].reference)
        await feed.upload(getPostageBatch(), updates[2].reference)
      }, FEED_TIMEOUT)

      it('should return false if no feed updates', async () => {
        const nonExistingTopic = randomByteArray(32, Date.now())

        await expect(bee.isFeedRetrievable('sequence', owner, nonExistingTopic)).resolves.toEqual(false)
      })

      it(
        'should return true for latest query for existing topic',
        async () => {
          await expect(bee.isFeedRetrievable('sequence', owner, existingTopic)).resolves.toEqual(true)
        },
        FEED_TIMEOUT,
      )

      it(
        'should return true for index based query for existing topic',
        async () => {
          await expect(bee.isFeedRetrievable('sequence', owner, existingTopic, '0000000000000000')).resolves.toEqual(
            true,
          )
          await expect(bee.isFeedRetrievable('sequence', owner, existingTopic, '0000000000000001')).resolves.toEqual(
            true,
          )
          await expect(bee.isFeedRetrievable('sequence', owner, existingTopic, '0000000000000002')).resolves.toEqual(
            true,
          )
        },
        FEED_TIMEOUT,
      )

      it(
        'should return false for index based query for existing topic but non-existing index',
        async () => {
          await expect(bee.isFeedRetrievable('sequence', owner, existingTopic, '0000000000000005')).resolves.toEqual(
            false,
          )
        },
        FEED_TIMEOUT,
      )
    })

    describe('topic', () => {
      it('create feed topic', () => {
        const topic = bee.makeFeedTopic('swarm.eth:application:handshake')
        const feed = bee.makeFeedReader('sequence', topic, owner)

        expect(feed.topic).toEqual(topic)
      })
    })
  })

  describe('soc', () => {
    const socHash = '9d453ebb73b2fedaaf44ceddcf7a0aa37f3e3d6453fea5841c31f0ea6d61dc85' as HexString

    describe('writer', () => {
      it('should read and write', async () => {
        const identifier = makeBytes(32) // all zeroes
        const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
        await tryDeleteChunkFromLocalStorage(socAddress)

        const socWriter = bee.makeSOCWriter(testIdentity.privateKey)

        const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
        expect(reference).toEqual(socHash)

        const soc = await socWriter.download(identifier)
        const payload = soc.payload()
        expect(payload).toEqual(testChunkPayload)
      })
    })

    describe('reader', () => {
      it('should read', async () => {
        const signer = makeSigner(testIdentity.privateKey)
        const identifier = makeBytes(32) // all zeroes
        const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
        await tryDeleteChunkFromLocalStorage(socAddress)
        await uploadSingleOwnerChunkData(BEE_KY, signer, getPostageBatch(), identifier, testChunkPayload)

        const socReader = bee.makeSOCReader(testIdentity.address)
        const soc = await socReader.download(identifier)
        const payload = soc.payload()
        expect(payload).toEqual(testChunkPayload)
      })
    })
  })

  describe('signer', () => {
    it('should be possible to pass it in constructor', async () => {
      const identifier = makeBytes(32)
      identifier[31] = 1

      const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
      await tryDeleteChunkFromLocalStorage(socAddress)

      const bee = new Bee(BEE_URL, { signer: testIdentity.privateKey })
      const socWriter = bee.makeSOCWriter()

      const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
      expect(reference).toEqual('00019ec85e8859aa641cf149fbd1147ac7965a9cad1dfe4ab7beaa12d5dc8027')
    })

    it('should prioritize signer passed to method', async () => {
      const identifier = makeBytes(32)
      identifier[31] = 2

      const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
      await tryDeleteChunkFromLocalStorage(socAddress)

      // We pass different private key to the instance
      const bee = new Bee(BEE_URL, {
        signer: '634fb5a872396d9611e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd',
      })
      const socWriter = bee.makeSOCWriter(testIdentity.privateKey)

      const reference = await socWriter.upload(getPostageBatch(), identifier, testChunkPayload)
      expect(reference).toEqual('d1a21cce4c86411f6af2f621ce9a3a0aa3cc5cea6cc9e1b28523d28411398cfb')
    })

    it('should throw if no signers are passed', () => {
      const bee = new Bee(BEE_URL)
      expect(() => bee.makeSOCWriter()).toThrow()
    })
  })

  describe('JsonFeed', () => {
    const TOPIC = 'some=very%nice#topic'

    it(
      'should set JSON to feed',
      async () => {
        await bee.setJsonFeed(getPostageBatch(), TOPIC, testJsonPayload, { signer: testIdentity.privateKey })

        const hashedTopic = bee.makeFeedTopic(TOPIC)
        const reader = bee.makeFeedReader('sequence', hashedTopic, testIdentity.address)
        const chunkReferenceResponse = await reader.download()
        expect(chunkReferenceResponse.reference).toEqual(testJsonHash)

        const downloadedData = await bee.downloadData(chunkReferenceResponse.reference)
        expect(downloadedData.json()).toEqual(testJsonPayload)
      },
      FEED_TIMEOUT,
    )

    it(
      'should get JSON from feed',
      async () => {
        const data = [{ some: { other: 'object' } }]

        const hashedTopic = bee.makeFeedTopic(TOPIC)
        const writer = bee.makeFeedWriter('sequence', hashedTopic, testIdentity.privateKey)
        const dataChunkResult = await bee.uploadData(getPostageBatch(), JSON.stringify(data))
        await writer.upload(getPostageBatch(), dataChunkResult.reference)

        const fetchedData = await bee.getJsonFeed(TOPIC, { signer: testIdentity.privateKey })
        expect(fetchedData).toEqual(data)
      },
      FEED_TIMEOUT,
    )
    it(
      'should get JSON from feed with address',
      async () => {
        const data = [{ some: { other: 'object' } }]

        const hashedTopic = bee.makeFeedTopic(TOPIC)
        const writer = bee.makeFeedWriter('sequence', hashedTopic, testIdentity.privateKey)
        const dataChunkResult = await bee.uploadData(getPostageBatch(), JSON.stringify(data))
        await writer.upload(getPostageBatch(), dataChunkResult.reference)

        const fetchedData = await bee.getJsonFeed(TOPIC, { address: testIdentity.address })
        expect(fetchedData).toEqual(data)
      },
      FEED_TIMEOUT,
    )
  })
})
Example #18
Source File: contract-files-reader.test.ts    From aloxide with Apache License 2.0 4 votes vote down vote up
describe('test contract-files-reader', () => {
  describe('readABIFile()', () => {
    it('should throw error when missing file path', () => {
      expect(() => {
        // @ts-ignore
        readABIFile(undefined);
      }).toThrowError('File path is required.');
    });

    it('should throw error when missing API instance', () => {
      expect(() => {
        // @ts-ignore
        readABIFile('some/path');
      }).toThrowError('Missing EOS API instance');
    });

    it('should read file and return serialized hex content', () => {
      const api = new Api({
        rpc: new JsonRpc('some_url', { fetch }),
        signatureProvider: new JsSignatureProvider([]),
        textEncoder: new TextEncoder(),
        textDecoder: new TextDecoder(),
      });

      const testFileBuffer = Buffer.from('{ "test": "test content" }', 'utf-8');
      const readFileSyncMock = jest
        .spyOn(fs, 'readFileSync')
        .mockReturnValueOnce(testFileBuffer.toString('utf-8'));

      const res = readABIFile('some/path', api);
      expect(typeof res).toBe('string');
    });
  });

  describe('readWASMFile()', () => {
    it('should throw error when missing file path', () => {
      expect(() => {
        // @ts-ignore
        readWASMFile(undefined);
      }).toThrowError('File path is required.');
    });

    it('should throw Not Found error when reading from not-existing file', () => {
      const path = 'not/existing/file/path';

      expect(() => {
        readWASMFile(path);
      }).toThrowError('File not found!');
    });

    it('should throw error when reading failed', () => {
      const path = 'not/existing/file/path';
      const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
        throw new Error('Test Error');
      });

      expect(() => {
        readWASMFile(path);
      }).toThrowError('Test Error');
      expect(readFileSyncMock).toBeCalledWith(path);
    });

    it('should read file and convert to hex string', () => {
      const testFileBuffer = Buffer.from('test content', 'utf-8');
      const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(testFileBuffer);
      const wasmPath = 'some/wasm/path';

      const result = readWASMFile(wasmPath);
      expect(result).toBe(testFileBuffer.toString('hex'));
      expect(readFileSyncMock).toBeCalledWith(wasmPath);
    });
  });

  describe('readPSFile()', () => {
    it('should throw error when missing file path', () => {
      expect(() => {
        // @ts-ignore
        readPSFile(undefined);
      }).toThrowError('File path is required.');
    });

    it('should throw Not Found error when reading from not-existing file', () => {
      const path = 'not/existing/file/path';

      expect(() => {
        readPSFile(path);
      }).toThrowError('File not found!');
    });

    it('should throw error when reading failed', () => {
      const path = 'not/existing/file/path';
      const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
        throw new Error('Test Error');
      });

      expect(() => {
        readPSFile(path);
      }).toThrowError('Test Error');
      expect(readFileSyncMock).toBeCalledWith(path);
    });

    it('should read file and convert to hex string', () => {
      const testFileBuffer = Buffer.from('test content', 'utf-8');
      const readFileSyncMock = jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(testFileBuffer);
      const psPath = 'some/ps/path';

      const result = readPSFile(psPath);
      expect(result).toBe(testFileBuffer.toString('hex'));
      expect(readFileSyncMock).toBeCalledWith(psPath);
    });
  });
});
Example #19
Source File: extension.ts    From checkov-vscode with Apache License 2.0 4 votes vote down vote up
// this method is called when extension is activated
export function activate(context: vscode.ExtensionContext): void {
    const logger: Logger = getLogger(context.logUri.fsPath, logFileName);
    logger.info('Starting Checkov Extension.', { extensionVersion, vscodeVersion: vscode.version });

    initializeStatusBarItem(OPEN_CONFIGURATION_COMMAND);
    let extensionReady = false;
    let checkovRunCancelTokenSource = new vscode.CancellationTokenSource();
    let checkovInstallation : CheckovInstallation | null = null;
    const checkovInstallationDir = vscode.Uri.joinPath(context.globalStorageUri, 'checkov-installation').fsPath;

    const resetCancelTokenSource = () => {
        checkovRunCancelTokenSource.cancel();
        checkovRunCancelTokenSource.dispose();
        checkovRunCancelTokenSource = new vscode.CancellationTokenSource();
    };

    // Set diagnostics collection
    const diagnostics = vscode.languages.createDiagnosticCollection('checkov-alerts');
    context.subscriptions.push(diagnostics);

    // Set commands
    context.subscriptions.push(
        vscode.commands.registerCommand(INSTALL_OR_UPDATE_CHECKOV_COMMAND, async () => {
            try {
                extensionReady = false;
                setSyncingStatusBarItem(checkovInstallation?.version, 'Updating Checkov');
                const checkovVersion = getCheckovVersion();
                checkovInstallation = await installOrUpdateCheckov(logger, checkovInstallationDir, checkovVersion);
                logger.info('Checkov installation: ', checkovInstallation);
                checkovInstallation.version = await runVersionCommand(logger, checkovInstallation.checkovPath, checkovVersion);

                const previousCheckovVersion = context.globalState.get(checkovVersionKey);
                if (previousCheckovVersion !== checkovInstallation.version) {
                    logger.info('Previously installed checkov version does not match the newly installed one. Clearing results cache.');
                    context.globalState.update(checkovVersionKey, checkovInstallation.version);
                    clearCache(context, logger);
                } else {
                    logger.debug('Previously installed checkov version matches the newly installed one');
                }

                setReadyStatusBarItem(checkovInstallation.version);
                extensionReady = true;
                if (vscode.window.activeTextEditor && isSupportedFileType(vscode.window.activeTextEditor.document.fileName))
                    vscode.commands.executeCommand(RUN_FILE_SCAN_COMMAND);
            } catch(error) {
                setErrorStatusBarItem(checkovInstallation?.version);
                logger.error('Error occurred while preparing Checkov. Verify your settings, or try to reload vscode.', { error });
                !shouldDisableErrorMessage() && showContactUsDetails(context.logUri, logFileName);
            }
        }),
        vscode.commands.registerCommand(RUN_FILE_SCAN_COMMAND, async (fileUri?: vscode.Uri): Promise<void> => {
            if (!extensionReady) {
                logger.warn('Tried to scan before checkov finished installing or updating. Please wait a few seconds and try again.');
                vscode.window.showWarningMessage('Still installing/updating Checkov, please wait a few seconds and try again.', 'Got it');
                return;
            }
            resetCancelTokenSource();
            await startScan(fileUri, true);
        }),
        vscode.commands.registerCommand(REMOVE_DIAGNOSTICS_COMMAND, () => {
            if (vscode.window.activeTextEditor) {
                setReadyStatusBarItem(checkovInstallation?.version);
                applyDiagnostics(vscode.window.activeTextEditor.document, diagnostics, []);
            }
        }),
        vscode.commands.registerCommand(OPEN_CONFIGURATION_COMMAND, () => {
            vscode.commands.executeCommand('workbench.action.openSettings', '@ext:Bridgecrew.checkov');
        }),
        vscode.commands.registerCommand(OPEN_EXTERNAL_COMMAND, (uri: vscode.Uri) => vscode.env.openExternal(uri)),
        vscode.commands.registerCommand(GET_INSTALLATION_DETAILS_COMMAND, async () => {
            if (!checkovInstallation || !checkovInstallation.version) {
                vscode.window.showWarningMessage("Checkov has not been installed. Try waiting a few seconds or running the 'Install or Update Checkov' command");
            } else {
                await showAboutCheckovMessage(checkovInstallation.version, checkovInstallation.checkovInstallationMethod);
            }
        }),
        vscode.commands.registerCommand(OPEN_CHECKOV_LOG, async () => {
            vscode.window.showTextDocument(vscode.Uri.joinPath(context.logUri, logFileName));
        }),
        vscode.commands.registerCommand(CLEAR_RESULTS_CACHE, async () => {
            clearCache(context, logger);
        })
    );

    vscode.commands.executeCommand(INSTALL_OR_UPDATE_CHECKOV_COMMAND);

    context.subscriptions.push(
        vscode.workspace.onDidChangeTextDocument(changeEvent => {
            if (!extensionReady) return;
            if ((vscode.window.activeTextEditor &&
                changeEvent.document.uri.toString() !== vscode.window.activeTextEditor.document.uri.toString())
                || !isSupportedFileType(changeEvent.document.fileName))
                return;
            vscode.commands.executeCommand(REMOVE_DIAGNOSTICS_COMMAND);
            // Run scan on enter (new line)
            if (!changeEvent.contentChanges.some(change => change.text.includes('\n'))) return;

            const tempFileUri: vscode.Uri = vscode.Uri.joinPath(context.globalStorageUri, tempScanFile);
            const text: string = changeEvent.document.getText();
            const stringBuffer: Uint8Array = new TextEncoder().encode(text);

            // Save changes in temp file
            vscode.workspace.fs.writeFile(tempFileUri, stringBuffer)
                .then(() => {
                    logger.debug('Saved temporary file, now scanning', { tempFile: tempFileUri.fsPath });
                    vscode.commands.executeCommand(RUN_FILE_SCAN_COMMAND, tempFileUri);
                }, error => {
                    logger.error('Error occurred trying to save temp file', { error });
                });
        }),
        vscode.workspace.onDidSaveTextDocument(saveEvent => {
            if (!extensionReady) return;
            if ((vscode.window.activeTextEditor && saveEvent.uri.toString() !== vscode.window.activeTextEditor.document.uri.toString())
                || !isSupportedFileType(saveEvent.fileName)) {
                setReadyStatusBarItem(checkovInstallation?.version);
                return;
            }
            vscode.commands.executeCommand(RUN_FILE_SCAN_COMMAND);
        }),
        vscode.window.onDidChangeActiveTextEditor(changeViewEvent => {
            if (!extensionReady) return;
            if (changeViewEvent && !isSupportedFileType(changeViewEvent.document.fileName)) {
                resetCancelTokenSource();
                setReadyStatusBarItem(checkovInstallation?.version);
                return;
            }
            vscode.commands.executeCommand(RUN_FILE_SCAN_COMMAND);
        })
    );

    // set code action provider
    context.subscriptions.push(
        vscode.languages.registerCodeActionsProvider([{ pattern: ' **/*.{tf,yml,yaml,json}' },{ pattern: '**/Dockerfile' }, { pattern: '**/*.{txt,json,sum,gradle,gradle.kts,properties,xml}' }, { pattern: '**/METADATA' }],
            fixCodeActionProvider(context.workspaceState), { providedCodeActionKinds: providedCodeActionKinds })
    );

    const startScan = async (fileUri?: vscode.Uri, useCache = false): Promise<void> => {
        resetCancelTokenSource();
        const token = assureTokenSet(logger, OPEN_CONFIGURATION_COMMAND, checkovInstallation);
        const prismaUrl = getPrismaUrl();
        const certPath = getPathToCert();
        const useBcIds = getUseBcIds();
        const debugLogs = getUseDebugLogs();
        const checkovVersion = getCheckovVersion();
        vscode.commands.executeCommand(REMOVE_DIAGNOSTICS_COMMAND);
        if (!fileUri && vscode.window.activeTextEditor && !isSupportedFileType(vscode.window.activeTextEditor.document.fileName, true))
            return;
        if (!!token && vscode.window.activeTextEditor) {
            if (useCache) {
                const fileToScan = fileUri?.fsPath || vscode.window.activeTextEditor.document.fileName;
                const hash = getFileHash(fileToScan);
                // fileUri will be non-null if we are scanning a temp (unsaved) file, so use the active editor filename for caching in this case
                const cachedResults = getCachedResults(context, hash, vscode.window.activeTextEditor.document.fileName, logger);
                if (cachedResults) {
                    logger.debug(`Found cached results for file: ${vscode.window.activeTextEditor.document.fileName}, hash: ${hash}`);
                    handleScanResults(fileToScan, vscode.window.activeTextEditor, context.workspaceState, cachedResults.results, logger);
                    return;
                } else {
                    logger.debug(`useCache is true, but did not find cached results for file: ${vscode.window.activeTextEditor.document.fileName}, hash: ${hash}`);
                }
            }
            await runScan(vscode.window.activeTextEditor, token, certPath, useBcIds, debugLogs, checkovRunCancelTokenSource.token, checkovVersion, prismaUrl, fileUri);
        }
    };

    const runScan = debounce(async (editor: vscode.TextEditor, token: string, certPath: string | undefined, useBcIds: boolean | undefined, debugLogs: boolean | undefined, cancelToken: vscode.CancellationToken, checkovVersion: string, prismaUrl: string | undefined, fileUri?: vscode.Uri): Promise<void> => {
        logger.info('Starting to scan.');
        try {
            setSyncingStatusBarItem(checkovInstallation?.version, 'Checkov scanning');
            const filePath = fileUri ? fileUri.fsPath : editor.document.fileName;
            const configPath = getConfigFilePath(logger);

            if (!checkovInstallation) {
                logger.error('Checkov is not installed, aborting scan.');
                return;
            }

            const checkovResponse = await runCheckovScan(logger, checkovInstallation, extensionVersion, filePath, token, certPath, useBcIds, debugLogs, cancelToken, configPath, checkovVersion, prismaUrl);
            handleScanResults(filePath, editor, context.workspaceState, checkovResponse.results.failedChecks, logger);
        } catch (error) {
            if (cancelToken.isCancellationRequested) {
                return;
            }

            setErrorStatusBarItem(checkovInstallation?.version);
            logger.error('Error occurred while running a checkov scan', { error });
            !shouldDisableErrorMessage() && showContactUsDetails(context.logUri, logFileName);
        }
    }, 300, {});

    const handleScanResults = (filename: string, editor: vscode.TextEditor, state: vscode.Memento, checkovFails: FailedCheckovCheck[], logger: Logger) => {
        saveCheckovResult(context.workspaceState, checkovFails);
        applyDiagnostics(editor.document, diagnostics, checkovFails);
        (checkovFails.length > 0 ? setErrorStatusBarItem : setPassedStatusBarItem)(checkovInstallation?.version);
        saveCachedResults(context, getFileHash(filename), editor.document.fileName, checkovFails, logger);
    };
}